public

A self-updating Twitter OAuth script

  • Download Gist
self-updating-twitter-oauth-script.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
<? php
 
// Register an app: http://dev.twitter.com/apps
define('CONSUMER_KEY', 'YOUR CONSUMER KEY');
define('CONSUMER_SECRET', 'YOUR CONSUMER SECRET');
 
define('TWITTER_TOKEN', '{TWITTER_TOKEN}');
define('TWITTER_TOKEN_SECRET', '{TWITTER_TOKEN_SECRET}');
 
$oauth = new OAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
 
/* authentication start */
$request_token = $oauth->getRequestToken('https://api.twitter.com/oauth/request_token');
$oauth->setToken($request_token['oauth_token'], $request_token['oauth_token_secret']);
 
$url = 'https://api.twitter.com/oauth/authorize?' . http_build_query(array('oauth_token' => $request_token['oauth_token']));
print "Opening $url\n";
system('open ' . escapeshellarg($url));
fwrite(STDOUT, "Enter the PIN: ");
$access_token = $oauth->getAccessToken('https://api.twitter.com/oauth/access_token', NULL, trim(fgets(STDIN)));
 
file_put_contents(__FILE__, preg_replace(
array('/\{TWITTER_TOKEN\}/', '/\{TWITTER_TOKEN_SECRET\}/', '!/\n*\* authentication start \*/.+?/\* authentication end \*/\n*!s'),
array($access_token['oauth_token'], $access_token['oauth_token_secret'], ''),
file_get_contents(__FILE__)));
 
print "The script has been updated: try again\n";
exit();
/* authentication end */
 
$oauth->setToken(TWITTER_TOKEN, TWITTER_TOKEN_SECRET);
$oauth->fetch('http://twitter.com/account/verify_credentials.json');
print_r(json_decode($oauth->getLastResponse()));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.