Skip to content

Instantly share code, notes, and snippets.

@tjlytle
Created June 8, 2011 16:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tjlytle/1014769 to your computer and use it in GitHub Desktop.
Save tjlytle/1014769 to your computer and use it in GitHub Desktop.
Command Line Script to get Access Token using Zend_Twitter
#!/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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment