1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Would you like to show me in a detailed example about how to obtain access token using twitteroauth? Their documentation is confusing.
I haven't used twitteroauth myself but Abraham Williams, the author of the library, created a GitHub repository with an example in PHP. You can also check tmhOAuth from Matt Harris, and some corresponding examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.