public
Last active

Command Line Script to get Access Token using Zend_Twitter

  • Download Gist
oauth.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#!/usr/bin/php
<?php
//consumer key and secret
$key = "APPKEY";
$secret = "APPSECRET";
 
require_once 'Zend/Oauth/Consumer.php';
 
//setup consumer like any other request, note that the 'oob' callbackUrl cannot
//be set at this point (will throw an exception)
$consumer = new Zend_Oauth_Consumer(array(
'siteUrl' => 'https://twitter.com/oauth',
'consumerKey' => $key,
'consumerSecret' => $secret
));
 
//get the request token, using the speciall 'oob' callbackUrl
$requestToken = $consumer->getRequestToken(array('callbackUrl' => 'oob'));
 
//output the link, and request the pin
echo "OAuth Link: {$consumer->getRedirectUrl()}" . PHP_EOL;
fwrite(STDOUT, "Enter the PIN: ");
 
//get the pin and use it to get an access token
$pin = trim(fgets(STDIN));
try{
//acting like a normal $_GET array, use the pin as the verifier, and add the
//token (since a normal oauth process would have passed the original token)
$accessToken = $consumer->getAccessToken(array('oauth_verifier' => $pin, 'oauth_token' => $requestToken->getToken()), $requestToken);
} catch (Zend_Oauth_Exception $e){
echo $e->getMessage() . PHP_EOL;
exit;
}
 
//output the access token and secret
echo "OAuth Token: {$accessToken->getToken()}" . PHP_EOL;
echo "OAuth Secret: {$accessToken->getTokenSecret()}" . PHP_EOL;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.