简体   繁体   中英

php twitter api - How to get access token?

I am using this code to generate access token:

if(isset($_POST['login_tw'])) {

$request_token = $connection->getRequestToken($_REQUEST['oauth_verifier']); 

    if($request_token)  {
        $token = $request_token['oauth_token'];
        $_SESSION['request_token'] = $token ;
        $_SESSION['request_token_secret'] = $request_token['oauth_token_secret'];

        if($connection->http_code = 200) 
        {
                $url = $connection->getAuthorizeURL($token);

                header('Location: ' . $url); 

                $fp=fopen('config.php','w');
                fwrite($fp, '<?php 
                $acsstok = "' . $_SESSION['request_token'] . '";
                $scrtaccstok = "' . $_SESSION['request_token_secret'] . '";
                ?>');
                fclose($fp);                    

        }

But the generated token can't be used for anything, especially to get a list of other user followers.

And the access token format is not like the one that is generated from 'Application Management' page that have leading numbers like this:

340708911-xVcFcjVjkdksfdrjhgjdfyhjdfhjdhdfkdjgkfvaJ

But it have no leading numbers like this:

xVcFcjVjkdksfdrjhgjdfyhjdfhjdhdfkdjgkfvaJ

What's wrong with the code above and how should it be so I can use the generated access token in my app?

Can somebody here help me? I have spending 1 day to find the solution for that problem.

Best Regards

For a Twitter user to become a user of your application, you must ask them for permission.

Once you have the request_token , you need to redirect the user to an authorization screen on Twitter where they can approve your application as described here: Obtaining Access Tokens .

After granting access to your application, the user will be redirected back to your application, specifically at the callback URL that you specified. On that page, you need capture the information Twitter sent to you (an oauth_verifier and the original request_token ) and then send another request to Twitter to exchange the request_token for an access_token . With this access_token , similar to the one you mentioned (with the User ID prefix), you will be able to perform authenticated requests to the Twitter API.

Please check Twitter Libraries if you would like to use existing third-party PHP libraries and simplify these authentication steps when integrating Twitter in your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM