Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter O-Auth Callback url

Tags:

oauth

twitter

I am having a problem with Twitter's oauth authentication and using a callback url.

I am coding in php and using the sample code referenced by the twitter wiki, http://github.com/abraham/twitteroauth

I got that code, and tried a simple test and it worked nicely. However I want to programatically specify the callback url, and the example did not support that.

So I quickly modified the getRequestToken() method to take in a parameter and now it looks like this:

function getRequestToken($params = array()) {
  $r = $this->oAuthRequest($this->requestTokenURL(), $params);
  $token = $this->oAuthParseResponse($r);
  $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  return $token;
}

and my call looks like this

$tok = $to->getRequestToken(array('oauth_callback' => 'http://127.0.0.1/twitter_prompt/index.php'));

This is the only change I made, and the redirect works like a charm, however I am getting an error when I then try and use my newly granted access to try and make a call. I get a "Could not authenticate you" error. Also the application never actually gets added to the users authorized connections.

Now I read the specs and I thought all I had to do was specify the parameter when getting the request token. Could someone a little more seasoned in oauth and twitter possibly give me a hand? Thank You

like image 961
jtymann Avatar asked Sep 28 '09 17:09

jtymann


2 Answers

I think this is fixed by twitter by now or you might have missed to provide a default callback url in your application settings, which is required for dynamic callback url to work as mentioned by others above.

Any case, I got this working by passing the oath_callback parameter while retrieving the request token. I am using twitter-async PHP library and had to make a small tweak to make the library pass the callback url.

If you are using twitter-async, the change is below:

modified getRequestToken and getAuthenticateURL functions to take callback url as parameter

 public function getRequestToken($callback_url = null)
  {
    $params = empty($callback_url) ? null : array('oauth_callback'=>$callback_url);
    $resp = $this->httpRequest('GET', $this->requestTokenUrl, $params);
    return new EpiOAuthResponse($resp);
  }


 public function getAuthenticateUrl($callback_url = null)
  { 
    $token = $this->getRequestToken($callback_url);
    return $this->authenticateUrl . '?oauth_token=' . $token->oauth_token;
  }

And pass the callback url from your PHP code.

$twitterObj->getAuthenticateUrl('http://localhost/twitter/confirm.php');
like image 162
Murukesh Avatar answered Jan 02 '23 20:01

Murukesh


@Ian, twitter now allows 127.0.0.1 and has made some other recent changes.

@jtymann, check my answer here and see if it helps

Twitter oauth_callback parameter being ignored!

GL

jingles

like image 28
jsh Avatar answered Jan 02 '23 18:01

jsh