5

I am working on twitter authentication, everything seems to work well till i get to getAccessToken which returns Array ( [ ] => ).

What I have done:

  1. First phase:

    $connection = new TwitterOAuth('xxxxxxxx','xxxxxxxx');
    
    $temporary_credentials = $connection->getRequestToken('http://example.com/profile.php?passurl=1');
    
    $redirect_url = $connection->getAuthorizeURL($temporary_credentials);
    
    $_COOKIE['oauth_token'] = $temporary_credentials['oauth_token'];
    
    $_COOKIE['oauth_token_secret'] = $temporary_credentials['oauth_token_secret'];
    
    header("Location: $redirect_url");
    
  2. second phase (this is where i encounter the problem)

    $connection =
            new TwitterOAuth(
                    'xxxxxxxxxx',
                    'xxxxxxxxxx',
                    $_COOKIE['oauth_token'],
                    $_COOKIE['oauth_token_secret']
                    ); 
    $token_credentials = $connection->getAccessToken();
    

I intend saving the $token_credentials values in the database but it returns an empty array: Array ( [ ] => )

What am i not getting right?

10
  • 2
    please next time try to indent your code before posting it, it was totally unreadable! Commented Apr 27, 2012 at 16:24
  • In twitteroauth.php add a var_dump($response); to the end of the http function. This will be the raw response returned from Twitter. Is it an error or a an access token? Commented Apr 29, 2012 at 22:54
  • this is what is returned: string(147) "oauth_token=xxxxstringxxxx&oauth_token_secret=xxxxstringxxxx&oauth_callback_confirmed=true" Commented Apr 29, 2012 at 23:17
  • @abraham returns: string(147) "oauth_token=xxxxstringxxxx&oauth_token_secret=xxxxstringxxxx&oauth_callback_con‌​firmed=true". thanks Commented Apr 29, 2012 at 23:29
  • That is the response to getRequestToken. What about for getAccessToken? Commented Apr 29, 2012 at 23:45

3 Answers 3

3
+50

Are you passing the "oauth_verifier" to the getAccessToken method? In the sample code you are giving, you are not doing it. Take a look at this diagram, specially on part D

OAuth Diagram

I have used twitteroauth in the past and I remember doing it like this. (starting second fase)

$twitterOauth = new TwitterOAuth($AppId, $twSecret, $_COOKIE['oauth_token'], $_COOKIE['oauth_token_secret']);
$twToken = $twitterOauth->getAccessToken($_REQUEST['oauth_verifier']);

$newTwitterOauth = new TwitterOAuth($AppId,  $twSecret, $twToken['oauth_token'], $twToken['oauth_token_secret']);
$response  = (array) $newTwitterOauth->get('account/verify_credentials');
var_dump($response);

There are a couple of suggestions I would do though:

  • Instead of stoing that information in a cookie, I strongly suggest you use sessions to do the job.
  • If you insist on using cookies, please use the setcookie function.
Sign up to request clarification or add additional context in comments.

1 Comment

yeah, you hit the two issues: 1. i was not setting the cookie the right way. 2. i was not passing the oauth _verifier to getAccessToken.
0

Where are you getting your TwitterOAuth class from? Is it a standard class? Or you've written it yourself? I would try debugging the class itself and putting more verbose output in the class functions.

5 Comments

am using this: github.com/abraham/twitteroauth... please kindly suggest any fix. thank you
Try submitting a question on github for that project. You might get better responses there, since it's a custom class.
is there any other available class one can use. I really need to get this working! arghh!!!
If you're writing a project from scratch I used thinphp.com and it worked quiet ok for me. Maybe you can extract the OAuthTwitter class from that project
Please i may not be able to extract the class from thin php framework
0

why are you using cookies? using cookies it is a potential risk because are stored on the client side, use SESSION instead

Another question: in teh $redirect_url if u make a print_r of the $_COOKIE['oauth_token'] and the another one, r empty?

Im asking this because if u disable the cookies by some addon ( like webdeveloper in firefox) or in the browsers config. Maybe u r using empty values on $_COOKIE['oauth_token'], and $_COOKIE['oauth_token_secret'] .

1 Comment

i am having issues setting SESSIONS on the site, so temporarily for testing am using COOKIES which actually holds the right values.

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.