Twitter O-Auth Callback url

2019-04-06 16:11发布

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

标签: twitter oauth
5条回答
Viruses.
2楼-- · 2019-04-06 16:13

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');
查看更多
干净又极端
3楼-- · 2019-04-06 16:14

Twitter does not honor the oauth_callback parameter and will only use the one specified in the registered application settings.

It also doesn't allow for 127.0.0.1 or localhost names in that callback, so I've setup http://dev.twipler.com which is setup for 127.0.0.1 in DNS so you can safely use;

http://dev.twipler.com/twitter_prompt/index.php
查看更多
来,给爷笑一个
4楼-- · 2019-04-06 16:20

even me to was getting 401 error.. but its resolved.. during registering your application to twitter you need to give callback url... like http://localhost:8080.

i have done this using java... so my code is: String CallbackURL="http://localhost:8080/tweetproj/index.jsp"; provider.retrieveRequestToken(consumer,CallbackURL);

where tweetproj is my project name and index.jsp is just one jsp page...

Hope this may helps u...

查看更多
一夜七次
5楼-- · 2019-04-06 16:35

After the user authorizes the application on twitter.com and they return to your callback URL you have to exchange the request token for an access token.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-04-06 16:40

@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

查看更多
登录 后发表回答