Created
September 10, 2010 05:51
-
-
Save abraham/573167 to your computer and use it in GitHub Desktop.
Example code for getting an OAuth 1.0a access_token from Twitter @anywhere users.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- User hits the oauth_callback_url with an oauth_access_token --> | |
http://abrah.am/#oauth_access_token=9436992-QKOV6cNsfeRe9Kagyy4AmQF7WX2efs3rRDgyEy7Bvsfeaf&oauth_bridge_code=JKOPZJbnQ7fPAQxFoYJasfsefSFefss7WDP7z5l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Use your oauth_callback_url and your oauth_client_identifier aka the @Anywhere API key --> | |
https://oauth.twitter.com/2/authorize?oauth_callback_url=http://abrah.am/&oauth_mode=flow_web_client&oauth_client_identifier=9QR94sYuXI3j6XkYrr1Ybw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 = $_REQUEST['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'); | |
// If the credentials are valid save them for future use. | |
} else { | |
// Something went wrong converting to an OAuth 1.0a token. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Bridge Code Example</title> | |
<!-- Be sure to use your own @Anywhere API key. --> | |
<script src="http://platform.twitter.com/anywhere.js?id=9QR94sYuXI3j6XkYrr1Ybw&v=1" type="text/javascript"></script> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> | |
</head> | |
<body> | |
<span id="twttr-anywhere-button"></span> | |
<script type="text/javascript"> | |
twttr.anywhere(function (T) { | |
if (T.isConnected()) { | |
$("#twttr-anywhere-button").append('<button id="signout" type="button">Sign out of Twitter</button>'); | |
$("#signout").bind("click", function () { | |
twttr.anywhere.signOut(); | |
location.reload(); | |
}); | |
} else { | |
T("#twttr-anywhere-button").connectButton({ | |
authComplete: function(user, bridge_code) { | |
$.post('convert.php', { | |
'bridge_code': bridge_code | |
}); | |
} | |
}); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$consumer_secret = 'consumer_secret'; | |
$twitter_anywhere_identity = explode(':', $_COOKIE['twitter_anywhere_identity']); | |
// sha1 of user_id and consumer_secret will match the value of the cookie after the : | |
if (sha1($twitter_anywhere_identity[0] . $consumer_secret) === $twitter_anywhere_identity[1]) { | |
echo 'User is verified.'; | |
} else { | |
echo 'User is NOT verified.'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment