twitteroauth icon indicating copy to clipboard operation
twitteroauth copied to clipboard

Problem with getAccessToken()

Open sainathk21 opened this issue 13 years ago • 12 comments

Hi,

I just realised I was using an outdated version of your library, so I replaced the old files with the new ones. After this change, this function has stopped working. It seems I'm missing an argument for this function below.

Function call is - $tok = $connection->getAccessToken($_REQUEST['oauth_verifier']);

It throws this error - stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [message] => Bad Authentication data [code] => 215 ) ) )

Im not sure why this is happening, is there somewhere I need to set "oauth_verifier" ?

Edit : I also tried to replace $_REQUEST with $_GET (To make it $_GET['oauth_verifier']), that didn't work either!

sainathk21 avatar Jul 30 '13 19:07 sainathk21

Hi,

Try this,

in the twitteroauth.php file change the function to

   function getAccessToken($oauth_verifier, $oauth_token) {
$parameters = array();
$parameters['oauth_verifier'] = $oauth_verifier;
$parameters['oauth_token'] = $oauth_token;
$request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
$token = OAuthUtil::parse_parameters($request);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
return $token;

}

and pass the values as below in the callback file

 $access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier'], $_GET['oauth_token']); 

--shiva

shiva1117 avatar Aug 02 '13 11:08 shiva1117

Hi Shiva,

Thanks for the response. I tried this, but oauth_verifier still has no value..

sainathk21 avatar Aug 03 '13 07:08 sainathk21

will you post your code here then?

shiva1117 avatar Aug 03 '13 10:08 shiva1117

oauth_verifier is a query param on the callback url Twitter sends users back to after the authorize access.

abraham avatar Aug 04 '13 00:08 abraham

Hi, Sorry for the late response.

@abraham : Thank you for that! I had immense difficulty understanding exactly what it was!

@siva1117 : I'll put up some code very shortly (Not at my workstation at the moment).

One quick question, does the user need to be logged into twitter if I have to get tweets?

The page in question has 2 features mainly.

  1. A column that shows the latest 20 tweets with the hashtag '#twitter'.
  2. An option to login and tweet directly from my site.

I understand that to do the second feature, the user needs to log in. But is feature 1 possible WITHOUT the user logging in? This was possible before in API 1.0, but it stopped working immediately when the API change happened.

sainathk21 avatar Aug 05 '13 06:08 sainathk21

Sorry, accidentally closed issue!

sainathk21 avatar Aug 05 '13 06:08 sainathk21

Sorry for the delay! This is the php code from my page. The rest is HTML. (Note : The last echo returns blank)

<?php session_start(); ?>
<?php require_once('config.php'); ?>
<?php require_once('twitterOAuth/twitterOAuth.php'); ?>
<?php require_once('twitterOAuth/OAuth.php'); ?>
<?php
error_reporting(E_ALL ^ E_NOTICE);


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']);
    $tok = $to->getAccessToken($_REQUEST['oauth_verifier']);


    $_SESSION['oauth_access_token'] = $token = $tok['oauth_token'];
    $_SESSION['oauth_access_token_secret'] = $tok['oauth_token_secret'];

} 

if (isset($_GET['oauth_token'])) {
  $ot = $_GET['oauth_token'];
}

$Content = $to->get('search/tweets', array('q' => '#Twitter'));
echo $_REQUEST['oauth_verifier'];

?>

sainathk21 avatar Aug 07 '13 08:08 sainathk21

i have got oauth_token,oauth_token_secret,oauth_verifier........but i cant got acces_token and access_token_secret..

these error displays

Notice: Undefined index: oauth_token in C:\wamp\www\twitteroauth\twitteroauth.php on line 112 Notice: Undefined index: oauth_token_secret in C:\wamp\www\twitteroauth\twitteroauth.php on line 112

this is my code ....

$cKey = "xxxxxxxxxxxxx"; $cSecret = "xxxxxxxxxxxxxxxxxxxxx"; $accessToken = "268742651-mMwcOQWtPS9JhArbB8NvJ8HjgmNvXWlgaRNCFYhF"; $accessTokenSecret ="gNaJR1IIdilBXy4BpCjhueUMHCMu3xuZyWmkeWnSxLg"; echo $OAuthToken = $_SESSION['oauth_token']; echo $OAuthSecret = $_SESSION['oauth_token_secret'].'
'; echo $oauth_verifier = $_GET['oauth_verifier'].'
'; date_default_timezone_set('Asia/Kolkata'); $to = new TwitterOAuth($cKey,$cSecret,$OAuthToken,$OAuthSecret); $access_token = $to->getAccessToken($_GET['oauth_verifier']); print_r($access_token);

print_r gives

Array ( [ "1.0" encoding="UTF-8"?> Invalid / expired Token /oauth/access_token?oauth_consumer_key=xxxxxxxxxxxxxxxxxx [amp;oauth_nonce] => f0680bc31608059f810925b3d6d4d632 [amp;oauth_signature] => 7renFf+9ookkp/7wujenJ1syr4k= [amp;oauth_signature_method] => HMAC-SHA1 [amp;oauth_timestamp] => 1376051857 [amp;oauth_token] => ayrLzgIDay4INlnUYAQc9knvcgyaLZhcZGJz6T324 [amp;oauth_verifier] => pASpN8E8OrmaBqPHLkgKzIxXpRCzi3QYnBuDwwPFfYQ [amp;oauth_version] => 1.0 )

kirandeepbhangu avatar Aug 09 '13 12:08 kirandeepbhangu

Well, it gives back an error "Invalid / expired Token" See where you send to token, you put it in a variable as amp;oauth_verifier and amp;oauth_signature etc, that should be without the amp; . So you are making an invalid request with different keys as the expected keys, causing Twitter not receiving your tokens and retuning "invalid tokens" --> no access tokens as result.

jelledelaender avatar Aug 09 '13 12:08 jelledelaender

hi, thanx for reply CodingMammoth

how can i overcome form this problem [amp; ??

kirandeepbhangu avatar Aug 09 '13 13:08 kirandeepbhangu

Not 100% sure, but I guess you have an issue with a configuration of your server (I see it's WAMP). I assume he is converting & to & before sending it to Twitter to be safe (like backslashing), where further a URL is generated like api.twitter.com?para1&para2... where '&' is valid and 'amp;' is left follwing the URL-standards. You should 'revert &' there back to simple & before sending to Twitter.

See http://php.net/manual/en/function.htmlspecialchars.php You should check your WAMP config if he converting this stuff. I don't have this issue with a regular unix-setup.

You can add some log stuff into the OAuth files (twitteroauth/twitteroauth.php) to detect where and when the & are converted to &

jelledelaender avatar Aug 09 '13 13:08 jelledelaender

kirendeepbhangu, Where did you get oauth_verifier?

sainathk21 avatar Aug 10 '13 06:08 sainathk21