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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#!/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;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.