Skip to content

Instantly share code, notes, and snippets.

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
require_once 'Zend/Oauth/Consumer.php';
require_once 'Zend/Console/Getopt.php';
//set valid config file types/extensions
$configTypes = array('ini', 'xml', 'json', 'yaml');
//setup command line opts
$opts = new Zend_Console_Getopt(array(
'key|k=w' => 'OAuth Key - required if not using a config file',
'secret|s=w' => 'OAuth Secret - required if not using a config file',
'config|c=s' => 'Config File - required if not using a key/secret pair',
'config-type|t=w' => 'Config Type ['.implode(', ', $configTypes).'] - defaults to extension of file',
'config-section|n=w' => 'Config Section - the section to use'));
//validate the passed options
try {
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
//ensure a config file or both key and secret have been passed
if(!isset($opts->c) AND (!isset($opts->k) OR !isset($opts->s))){
echo $opts->getUsageMessage();
echo "A key/secret pair or a config file are required." . PHP_EOL;
//load the config file if passed
//allow type to be manually set
$type = $opts->t;
//or default to file's extension
} else {
$info = pathinfo($opts->c);
$type = $info['extension'];
//ensure config type is valid
$type = strtolower($type);
if(!in_array($type, $configTypes)){
echo $opts->getUsageMessage();
echo "$type is not a valid config file type." . PHP_EOL;
//determine the file to include/config class to use
$type = ucfirst($type);
require_once "Zend/Config/$type.php";
$class = "Zend_Config_$type";
//read the config file and get the consumer data
$config = new $class($opts->c, $opts->n);
if(!isset($config->oauth->consumer->key) OR !isset($config->oauth->consumer->secret)){
echo $opts->getUsageMessage();
echo "could not find oauth.consumer configuration" . PHP_EOL;
$key = $config->oauth->consumer->key;
$secret = $config->oauth->consumer->secret;
//use a consumer key/secret passed
} else {
$key = $opts->k;
$secret = $opts->s;
//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' => '',
'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));
//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;
//write to config file
//relaod the config file for writing
$config = new $class(
array('skipExtends' => true, 'allowModifications' => true));
//jump through a few hoops if the config file uses sections
$config->{$opts->n}->oauth = array();
$config->{$opts->n}->oauth->access = array();
$config->{$opts->n}->oauth->access->token = $accessToken->getToken();
$config->{$opts->n}->oauth->access->secret = $accessToken->getTokenSecret();
} else {
$config->oauth->access = array();
$config->oauth->access->token = $accessToken->getToken();
$config->oauth->access->secret = $accessToken->getTokenSecret();
//write the new config file
require_once "Zend/Config/Writer/$type.php";
$class = "Zend_Config_Writer_$type";
$writer = new $class(array('config' => $config, 'filename' => $opts->c));
//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