Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created September 11, 2010 12:35
Show Gist options
  • Select an option

  • Save thinkphp/575151 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/575151 to your computer and use it in GitHub Desktop.
Example code for getting an OAuth 1.0a access_token from Twitter @anywhere users.
<?php
// Download TwitterOAuth from http://github.com/abraham/twitteroauth.
require_once('twitteroauth/twitteroauth.php');
// Get consumer keys from http://dev.twitter.com/apps.
$consumer_key = '';
$consumer_secret = '';
$oauth_bridge_code = '';
// Build a TwitterOAuth object.
$connection = new TwitterOAuth(
$consumer_key,
$consumer_secret
);
// Request an access_token with an oauth_bridge_code.
$request = $connection->oAuthRequest('https://api.twitter.com/oauth/access_token', 'POST', array('oauth_bridge_code' => $oauth_bridge_code));
// If the access_token request succeeds...
if (200 === $connection->http_code){
// Parse the access_token string.
$token = OAuthUtil::parse_parameters($request);
// Build new TwitterOAuth object with users access_token.
$connection = new TwitterOAuth(
$consumer_key,
$consumer_secret,
$token['oauth_token'],
$token['oauth_token_secret']
);
// Call verify_credentials to make sure access_tokens are valid.
$content = $connection->get('account/verify_credentials');
} else {
$content = 'Oops.... something went wrong.';
}
print_r($content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment