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
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 {
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//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;
exit;
}
//load the config file if passed
if(isset($opts->c)){
//allow type to be manually set
if(isset($opts->t)){
$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;
exit;
}
//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;
exit;
}
$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' => '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;
}
//write to config file
if(isset($opts->c)){
//relaod the config file for writing
$config = new $class(
$opts->c,
null,
array('skipExtends' => true, 'allowModifications' => true));
//jump through a few hoops if the config file uses sections
if(isset($opts->n)){
if(!isset($config->{$opts->n}->oauth)){
$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));
$writer->write();
}
//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