dongxu3029 2011-08-31 07:45
浏览 56
已采纳

PHP Twitter API(亚伯拉罕) - 无法验证问题

I am having some problems with implementing the twitter api. I am using Abraham's method to connect to Twitter but I get this error:

object(stdClass)#25 (2) {
  ["error"]=>
  string(27) "Could not authenticate you."
  ["request"]=>
  string(302) "/1/account/verify_credentials.json?oauth_consumer_key=79CrBl2oYlaATF7YOn0New&oauth_nonce=014df8945398b9605b78703829f93c45&oauth_signature=7OnRBeraaGxXgLbTNQ3%2BFdMAIPs%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1314776180&oauth_token=U7iPIyqv7IJyq1VkvKsf06KE6KQOtVDPBEwwqRjhg&oauth_version=1.0"
}

This is my code:

$to = new TwitterOAuth();
        $tok = $to->getRequestToken(CURRURL);
        $request_link = $to->getAuthorizeURL($tok);
        $_SESSION['oauth_access_token']= $tok['oauth_token'];
        $_SESSION['oauth_access_token_secret'] = $tok['oauth_token_secret'];

if ((!isset($_SESSION['oauth_access_token'])) || ($_SESSION['oauth_access_token'])=='') {

            $to = new TwitterOAuth($_SESSION['oauth_access_token'], $_SESSION['oauth_request_token_secret']);
            $token_credentials = $to->getAccessToken($_REQUEST['oauth_verifier']);
            $_SESSION['oauth_access_token'] =$token_credentials['oauth_token'];
            $_SESSION['oauth_access_token_secret'] = $token_credentials['oauth_token_secret'];
        } else {
            $connection = new TwitterOAuth($access_token['oauth_token'], $access_token['oauth_token_secret']);
            $user_info = $to->get('account/verify_credentials');

            //die(var_dump($user_info));

            $data = array(
            "twitter" => array(
                "oauth_provider" => "twitter",
                "oauth_uid" => $user_info->id,
                "first_name" => $user_info->name,
                "last_name" => "",
                "username" => $user_info->screen_name,
                "email" => "",
                "gender" => "",
                "hometown" => "",
                "location" => $user_info->location
                )
            );

            array_push($social,$data);

        }

I have been banging my head to the wall over this, so any suggestions would greatly be appreciated!

  • 写回答

2条回答 默认 最新

  • dongwubao4785 2011-08-31 12:32
    关注

    First, when you initialize your method TwitteroAuth you've to pass also your consumer_key and consumer_secret that Twitter gives you.

    Second, after the getAuthorizeURL you've to redirect the user to the Twitter website to allow them to accept you access to their information.

    After the user allows Twitter to use its own information, Twitter redirects the user to your callback page. In your example is what contains the constant CURRURL.

    You'll receive two new information with your URL the oauth_token and oauth_token_secret and you gonna initialize TwitterOAuth again with this, the consumer_key, consumer_secret, and theses two new parameters. TwitterOAuth(key, secret, token, token_secret)

    And only after it you'll be able to get the user information with $user_info = $to->get('account/verify_credentials');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟