开发者

Zend OAuth with twitter single access tokens

I'm developing an application where it requires users to sign in using twitter and 开发者_StackOverflowOAuth. Everything works just fine thanks to Zend_OAuth. The problem is that the web app will also make some calls to the twitter API but these will be handled internally without the need of authenticating. Twitter Dev provides an 'access_token' and 'access_token_secret' keys for this purpose (Mentioned here http://dev.twitter.com/pages/oauth_single_token )

The problem is that I can't get Zend_OAuth to play nice with existing access tokens. Is there anyone who can help with a few lines of code how can I make an API call with my existing keys without being redirected to Twitter's Authorize page ? I know it can be done.

PS. If possible, I don't want to use external libraries like the ones mentioned in dev.twitter.com - just what Zend Framework is offering

Thanks!


With ZF version 1.10.8 minimal code required to post on Twitter is:

$token = new Zend_Oauth_Token_Access;
$token->setParams(array(
'oauth_token' => 'REPLACE_WITH_TOKEN',
'oauth_token_secret' => 'REPLACE_WITH_TOKEN_SECRET'
));

$twitter = new Zend_Service_Twitter(array(
'consumerKey' => 'REPLACE_WITH_CONSUMER_KEY', 
'consumerSecret' => 'REPLACE_WITH_CONSUMER_SECRET',
'accessToken' => $token
));

$response = $twitter->status->update('REPLACE WITH MESSAGE');

All tokens and secrets can be accessed after registering your application on http://dev.twitter.com


After re-generating the access_token obj from the db-stored token and tokensecret components (just as Justin showed), you may, alternatively, depending on your Twitter needs, bypass Zend_Oauth_Consumer and Zend_Service_Twitter and use the Zend_Http_Client available through Zend_Oauth_Token_Access. This works well for simple status posting - and that's what shown below:

$token = new Zend_Oauth_Token_Access();
$token->setToken('singletoken');
$token->setTokenSecret('singlesecret');

$client = $token->getHttpClient(array('requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER, 
'version' => '1.0', 
'signatureMethod' => 'HMAC-SHA1', 
'callbackUrl' => 'http://example.org/dummyurl', 
'requestTokenUrl' => 'http://twitter.com/oauth/request_token', 
'accessTokenUrl' => 'http://twitter.com/oauth/access_token', 
'userAuthorizationUrl' => 'http://twitter.com/oauth/authorize', 
'consumerKey' => 'yourconsumerkey',
'ConsumerSecret' => 'yourconsumersecret',
));

$client->setUri('http://twitter.com/statuses/update.json');
$client->setMethod(Zend_Http_Client::POST);
$client->setParameterPost('status', $_POST["tweet_text"]);
$response = $client->request();

$data = Zend_Json::decode( $response->getBody(), Zend_Json::TYPE_OBJECT);

if($data->text) { /* success */ }


$token = new Zend_Oauth_Token_Access();
$token->setToken('singletoken');
$token->setTokenSecret('singlesecret');
$consumer = new Zend_Oauth_Consumer(
array('requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER, 
'version' => '1.0', 
'signatureMethod' => 'HMAC-SHA1', 
'callbackUrl' => 'http://example.org/dummyurl', 
'requestTokenUrl' => 'http://twitter.com/oauth/request_token', 
'accessTokenUrl' => 'http://twitter.com/oauth/access_token', 
'userAuthorizationUrl' => 'http://twitter.com/oauth/authorize', 
'consumerKey' => 'yourconsumerkey',
'ConsumerSecret' => 'yourconsumersecret',
));

$twtr = new Zend_Service_Twitter(array('accessToken'=>$token), $consumer);

Should do it.


If the code sample does not work, try changing case of "ConsumerSecret" to "consumerSecret". It took me a few hours to catch this. Hopefully it saves your time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜