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?

Ogugua Belonwu
  • 2,111
  • 6
  • 28
  • 45
  • 2
    please next time try to indent your code before posting it, it was totally unreadable! – m09 Apr 27 '12 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? – abraham Apr 29 '12 at 22:54
  • this is what is returned: string(147) "oauth_token=xxxxstringxxxx&oauth_token_secret=xxxxstringxxxx&oauth_callback_confirmed=true" – Ogugua Belonwu Apr 29 '12 at 23:17
  • @abraham returns: string(147) "oauth_token=xxxxstringxxxx&oauth_token_secret=xxxxstringxxxx&oauth_callback_con‌​firmed=true". thanks – Ogugua Belonwu Apr 29 '12 at 23:29
  • That is the response to getRequestToken. What about for getAccessToken? – abraham Apr 29 '12 at 23:45
  • @abraham getAccessToken returns: string(1) " " – Ogugua Belonwu Apr 30 '12 at 00:10
  • Can you do a [gist](https://gist.github.com/) of `var_dump($connection);` after `$connection->getAccessToken();`. Be sure to strip oauth secrets. – abraham Apr 30 '12 at 00:30
  • It is very odd that it is an empty string as Twitter usually at least returns an error. – abraham Apr 30 '12 at 00:31
  • @abraham Link to gist: for var_dump($connection) - https://gist.github.com/2554619 . for $connection->getAccessToken() plus my twitter authorize function - https://gist.github.com/2554656. thank you so much for the assistance – Ogugua Belonwu Apr 30 '12 at 01:14
  • @abraham please use this for the var_dump($connection): https://gist.github.com/2554701; for $connection->getAccessToken() plus my twitter authorize function - https://gist.github.com/2554656 ... thanks – Ogugua Belonwu Apr 30 '12 at 01:33

3 Answers3

3

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.
mpratt
  • 1,598
  • 12
  • 21
  • 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. – Ogugua Belonwu May 06 '12 at 15:50
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.

Reza S
  • 9,480
  • 3
  • 54
  • 84
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'] .

J.K.A.
  • 7,272
  • 25
  • 94
  • 163
Quaid
  • 333
  • 1
  • 8
  • i am having issues setting SESSIONS on the site, so temporarily for testing am using COOKIES which actually holds the right values. – Ogugua Belonwu May 05 '12 at 19:09