Need help in Twitter api (Abraham's TwitterOAuth: Could Not Authenticate You Problem)

Discussion in 'PHP' started by Zetiz, Aug 25, 2011.

  1. #1
    Well,

    I am using Abraham's TwitterOauth library to build an application that will collect oauth token and oauth token secret for each user and save it to database. So that later I can tweet on behalf of them.

    I am doing it correctly. When user clicks on Allow this Application my app is getting authorized and user is redirected to the callback. I am also getting the Oauth token and Oauth token secret. But it's not working when it comes to update status or read timeline or do anything else with user's account.

    I have read and write access in settings. My consumer keys and secret are correct.

    It says Could Not Authenticate You.. What might be the problem? Please see my code below-

    Here is the code for index.php (URL for the application):
    <?php
    session_start();
    require_once 'twitteroauth.php';
    require_once 'OAuth.php';
    define("CONSUMER_KEY", "My key here");
    define("CONSUMER_SECRET", "my secret here");
    $to = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    $tok = $to->getRequestToken($_REQUEST['oauth_verifier']);
    $request_link = $to->getAuthorizeURL($tok);
    $_SESSION['oauth_access_token']= $tok['oauth_token'];
    $_SESSION['oauth_access_token_secret'] = $tok['oauth_token_secret'];
    header("Location: $request_link");
    exit;
    ?>
    PHP:
    Code For callback.php:

    
    <?php
    session_start();
    require_once 'twitteroauth.php';
    require_once 'OAuth.php';
    define("CONSUMER_KEY", "My Key");
    define("CONSUMER_SECRET", "My Secret");
    if ((!isset($_SESSION['oauth_access_token'])) || ($_SESSION['oauth_access_token'])=='') 
    {
    $to = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_request_token'], $_SESSION['oauth_request_token_secret']);
    $token_credentials = $to->getAccessToken($_REQUEST['oauth_verifier']);
    $_SESSION['oauth_access_token'] =$token_credentials['oauth_token'];
    $_SESSION['oauth_access_token_secret'] = $token_credentials['oauth_token_secret'];
    }
    $to=new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET,$_SESSION['oauth_access_token'],$_SESSION['oauth_access_token_secret']);
    $content = $to->post('statuses/update', array('status' => 'Hello Twitter')); 
    var_dump($content);
    ?>
    
    PHP:
    It gives the following Output:

    object(stdClass)#5 (2) { ["error"]=> string(27) "Could not authenticate you." ["request"]=> string(23) "/1/statuses/update.json" }

    It says "Could Not Authenticate You"...

    Any help will be highly appreciated..

    Thanks A Lot!
     
    Solved! View solution.
    Zetiz, Aug 25, 2011 IP
  2. Abraham Williams

    Abraham Williams Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #2
    "Could Not Authenticate You" means the access_token being used is invalid. You are likely overwriting it at some point with a new request_token.
     
    Abraham Williams, Aug 25, 2011 IP
  3. Zetiz

    Zetiz Active Member

    Messages:
    668
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Hello Abraham

    It is very nice to talk to you. But I am using the above two files only. Could you please look into the code and point out where I am doing wrong? I am unable to find where the access token is getting overwritten. :(




     
    Zetiz, Aug 26, 2011 IP
  4. #4
    In the first file you are saving the request_token in the session as oauth_access_token but in the second file you are trying to use the request_token from oauth_request_token.
     
    Abraham Williams, Aug 26, 2011 IP
  5. Zetiz

    Zetiz Active Member

    Messages:
    668
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Thanks a lot Abraham.. I figured it out. It is working now! :)
     
    Zetiz, Aug 26, 2011 IP