首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Twitter从from令牌和密钥获取用户访问令牌和访问令牌密钥

Twitter从from令牌和密钥获取用户访问令牌和访问令牌密钥
EN

Stack Overflow用户
提问于 2012-04-17 20:28:30
回答 1查看 5.9K关注 0票数 3

下面是我用来获得oauth和oauth secret.How的东西我能得到用户访问令牌和访问令牌秘密吗?

我没有看到文档中提到的任何使用上述令牌来实现此目的的过程

代码语言:javascript
复制
 require("twitteroauth/twitteroauth.php");
// The TwitterOAuth instance
$twitteroauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');
// Requesting authentication tokens, the parameter is the URL we will be redirected to
$request_token = $twitteroauth->getRequestToken('localhost.com/…');

// Saving them into the session
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

// If everything goes well..
if($twitteroauth->http_code==200){
// Let's generate the URL and redirect
$url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']); header('Location: '. $url); } else { // It's a bad idea to kill the script, but we've got to know when there's an error.
die('Something wrong happened.');
}

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-17 20:54:41

完整的程序(我相信我的twitterOAuth库和你的是一样的)

代码语言:javascript
复制
<?php 
/**
 * twitter_go.php -- users opens it and gets redirected to twitter.com
 */

    require (dirname(__FILE__) . '/twoauth/twitterOAuth.php');

    $consumer_key = '123abcd';
    $consumer_secret = '1234567890';



    $to = new TwitterOAuth($consumer_key, $consumer_secret);

    $tok = $to->getRequestToken();

    $token = $tok['oauth_token'];
    $secret = $tok['oauth_token_secret'];
    $time = $_SERVER['REQUEST_TIME'];

    //temporary things, i will need them in next function, so i put them in cookie
    setcookie("ttok", $token, $time + 3600 * 30, '/'); //,'.domain.com'); //migh need to add domain if got problems
    setcookie("tsec", $secret, $time + 3600 * 30, '/'); //,'.domain.com');

    $request_link = $to->getAuthorizeURL($token);

    //die($request_link);
    header('Location: ' . $request_link);


?>

<?php 
    /**
     * twitter_back.php  -- users gets redirected here from twitter (if user allowed you app)
     * you can specify this url in https://dev.twitter.com/
     */

        require (dirname(__FILE__) . '/twoauth/twitterOAuth.php');

        $consumer_key = '123abcd';
        $consumer_secret = '1234567890';


        $oauth_token = $_GET['oauth_token'] //  http://domain.com/twitter_back.php?oauth_token=MQZFhVRAP6jjsJdTunRYPXoPFzsXXKK0mQS3SxhNXZI&oauth_verifier=A5tYHnAsbxf3DBinZ1dZEj0hPgVdQ6vvjBJYg5UdJI

        if(!isset($_COOKIE['ttok'])) {
            die('no cookie, no party');
        }
        $ttok = $_COOKIE['ttok'];
        $tsec = $_COOKIE['tsec'];


        $to = new TwitterOAuth($consumer_key, $consumer_secret, $ttok, $tsec);
        $tok = $to->getAccessToken();
        if(!isset($tok['oauth_token'])) {
            die('try again!');
        }
        $btok = $tok['oauth_token'];
        $bsec = $tok['oauth_token_secret'];



        $to = new TwitterOAuth($consumer_key, $consumer_secret, $btok, $bsec);
        $content = $to->OAuthRequest('http://twitter.com/account/verify_credentials.xml');
        $user = simplexml_load_string($content);
        $userid = $user->id . ''; //typecast to string
        $screen_name = $user->screen_name . '';



        //delete temp. cookies
        setcookie("ttok", '', 0, '/');
        setcookie("tsec", '', 0, '/');


        /**
         * at this point you have everything on this user
         */
         print_r($user);
 ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10191276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档