Skip to content

Instantly share code, notes, and snippets.

@AlexanderAA
Created April 18, 2010 21:09
Show Gist options
  • Save AlexanderAA/370552 to your computer and use it in GitHub Desktop.
Save AlexanderAA/370552 to your computer and use it in GitHub Desktop.
def get_request_token(self, callback_url):
""" Get request token.
Request token will be used to construct authorization URL later.
Args:
callback_url - basestring, not urlencoded.
Example: http://entrecard.com/oauth/callback
Returns:
request_token of type oauth2.Token
or
None if request token was not received from server
Raises:
-
"""
parameters = dict(oauth_consumer_key = self.consumer.key,
oauth_signature_method = self.signature_method.name,
oauth_callback = callback_url,
oauth_timestamp = str(int(time.time())),
oauth_nonce = str(int(time.time())))
oauth_request = oauth2.Request(method = 'GET',
url=self.request_url,
parameters = parameters)
oauth_request.sign_request(self.signature_method, self.consumer, None)
url_params = urllib.urlencode(oauth_request)
response = self.read_response(self.request_url, url_params)
response_dict = self.validate_keys(response, ['oauth_token', 'oauth_token_secret'])
if response_dict:
return oauth2.Token(response_dict["oauth_token"], response_dict["oauth_token_secret"])
def get_authorization_url(self, callback_url):
""" Gets request token from server and composes authorization URL using
request token
Returns:
authorization_url (basestring) - URL, which is used in step 2 of workflow
(see docstring for parent class)
OR
None if request token was not obtained.
"""
request_token = self.get_request_token(callback_url)
if request_token:
oauth_request = oauth2.Request.from_consumer_and_token(
self.consumer,
token = request_token,
http_url = self.authorization_url)
oauth_request.sign_request(self.signature_method, self.consumer, request_token)
# User will be redirected to this URL
return '%s?%s' % (self.authorization_url, urllib.urlencode(oauth_request))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment