Created
July 11, 2011 18:43
-
-
Save gorakhargosh/1076495 to your computer and use it in GitHub Desktop.
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
from pyoauth.oauth1 import Credentials | |
from pyoauth.oauth1.client import Client | |
client_credentials = Credentials( | |
identifier="CONSUMER KEY", | |
shared_secret="CONSUMER SECRET" | |
) | |
oauth_client = Client( | |
client_credentials, | |
temporary_credentials_request_uri="REQUEST TOKEN URL", | |
token_credentials_request_uri="ACCESS TOKEN URL", | |
resource_owner_authorization_uri="AUTHORIZATION URL", | |
use_authorization_header=True # will use the "Authorization" HTTP header for OAuth parameters. | |
) | |
http_client = None # Use your HTTP client. | |
class LoginHandler(RequestHandler): | |
def get(self): | |
# Ask the OAuth server for the "request token" | |
req = oauth_client.build_temporary_credentials_request( | |
realm="Photos", | |
oauth_callback="CALLBACK URL" | |
) | |
response = http_client.request( | |
method=req.method, | |
payload=req.payload, | |
headers=req.headers, | |
url=req.url | |
) | |
# Extract the request token from the OAuth server's response. | |
response = ResponseProxy( | |
response.status_code, | |
response.status, | |
response.body, | |
response.headers | |
) | |
_, request_token = client.parse_temporary_credentials_response(response) | |
# Send (redirect) the user to the authorization URL created from the request token. | |
self.redirect(client.get_authorization_url(request_token)) | |
# Wait for the browser to return user to your callback URL. | |
# Handle the callback URL taking these query params out. | |
# In your callback handler | |
class MyCallbackHandler(RequestHandler): | |
def get(self): | |
oauth_token = self.request.get('oauth_token') | |
oauth_verifier = self.request.get('oauth_verifier') | |
# You need to check the verification code before you | |
# ask the server for the "access token". | |
oauth_client.check_verification_code(request_token, oauth_token, oauth_verifier) | |
# Now ask for the "Access token". | |
req = oauth_client.build_token_credentials_request( | |
realm="Photos", | |
request_token, | |
oauth_verifier | |
) | |
response = http_client.request( | |
method=req.method, | |
payload=req.payload, | |
headers=req.headers, | |
url=req.url | |
) | |
# Extract the "access token" from the response. | |
response = ResponseProxy( | |
response.status_code, | |
response.status, | |
response.body, | |
response.headers | |
) | |
_, access_token = oauth_client.parse_token_credentials_response(response) | |
# Save this access token to your database somewhere. | |
user.access_token = access_token | |
user.put() | |
# To use the access token (in this or any other handler) | |
req = oauth_client.build_resource_request( | |
access_token, | |
url="/api/username", | |
method="GET", | |
) | |
response = http_client.request( | |
method=req.method, | |
url=req.url, | |
headers=req.headers, | |
payload=req.payload | |
) | |
# Do what you want with the response here. | |
# .... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment