Created
July 27, 2011 23:54
-
-
Save gorakhargosh/1110624 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import webbrowser | |
| from pprint import pprint | |
| import httplib2 | |
| import sys | |
| httplib2.debuglevel = 1 | |
| from pyoauth.oauth1 import Credentials | |
| from pyoauth.oauth1.client import Client | |
| from pyoauth.httplib2.httpclient import HttpClient | |
| client_info = { | |
| 'twitter': { | |
| 'method': 'GET', | |
| 'params': dict( | |
| client_credentials=Credentials('CONSUMER-KEY', | |
| 'CONSUMER-SECRET'), | |
| temporary_credentials_uri=\ | |
| 'https://api.twitter.com/oauth/request_token', | |
| token_credentials_uri=\ | |
| 'https://api.twitter.com/oauth/access_token', | |
| authorization_uri=\ | |
| 'https://api.twitter.com/oauth/authorize', | |
| authentication_uri=\ | |
| 'https://api.twitter.com/oauth/authenticate', | |
| ), | |
| 'temporary_credentials_params': dict(), | |
| 'headers': {} | |
| }, | |
| 'google': { | |
| 'method': 'POST', | |
| 'params': dict( | |
| client_credentials=Credentials('CONSUMER-KEY', | |
| 'CONSUMER-SECRET'), | |
| temporary_credentials_uri=\ | |
| 'https://www.google.com/accounts/OAuthGetRequestToken', | |
| token_credentials_uri=\ | |
| 'https://www.google.com/accounts/OAuthGetAccessToken', | |
| authorization_uri=\ | |
| 'https://www.google.com/accounts/OAuthAuthorizeToken', | |
| ), | |
| 'temporary_credentials_params': dict( | |
| scope='https://docs.google.com/feeds/', | |
| ), | |
| 'headers': dict() | |
| } | |
| } | |
| def main(service_name): | |
| service = client_info[service_name] | |
| oauth = Client(HttpClient(), **service["params"]) | |
| # Fetch temporary credentials. | |
| response = oauth.fetch_temporary_credentials( | |
| method=service["method"], | |
| params=service["temporary_credentials_params"], | |
| headers=service["headers"], | |
| ) | |
| print("Temporary credentials response: %r" % response.body) | |
| temporary_credentials, _ = \ | |
| oauth.parse_temporary_credentials_response(response, False) | |
| # Open the web browser to allow the user to authorize and grab | |
| # the verification code. (Emulates redirect). | |
| if "authentication_uri" in service["params"]: | |
| webbrowser.open(oauth.get_authentication_url(temporary_credentials), | |
| new=2, autoraise=True) | |
| else: | |
| webbrowser.open(oauth.get_authorization_url(temporary_credentials), | |
| new=2, autoraise=True) | |
| # Get the verification code. | |
| oauth_verifier = oauth.check_verification_code(temporary_credentials, | |
| temporary_credentials.identifier, | |
| raw_input("Enter verification code: ")) | |
| # Now fetch the token credentials. | |
| response = oauth.fetch_token_credentials(temporary_credentials, | |
| oauth_verifier, | |
| method=service["method"]) | |
| print("Token credentials response: %r" % response) | |
| token_credentials, _ = oauth.parse_token_credentials_response( | |
| response, | |
| False | |
| ) | |
| # Whee. There are your token credentials, to make more requests | |
| # using oauth.fetch(). | |
| print("Here are your token credentials (access token/secret pair).") | |
| pprint(token_credentials.to_dict()) | |
| if __name__ == "__main__": | |
| main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment