views:

408

answers:

3

Hey all,

I'm developing an application where it requires users to sign in using twitter and OAuth. 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!

+1  A: 
$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.

Justin
+1  A: 

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 */ }
Juggernt
Thanks guys, both your answers where really helpful!
remedix
+2  A: 

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(
'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

forcecode