Created
October 24, 2013 03:59
-
-
Save chriskief/7131132 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
import urllib2 | |
import urlparse | |
import time | |
import oauth2 as oauth | |
from django.conf import settings | |
from django.core.urlresolvers import reverse | |
from django.core.context_processors import csrf | |
def get_authorization_url(request): | |
# URL to where we will redirect to | |
redirect_url = settings.SITE_URL + reverse('register_yahoo') | |
# set the api URL | |
url = 'https://api.login.yahoo.com/oauth/v2/get_request_token' | |
# required params for yahoo | |
params = { | |
'oauth_callback': redirect_url, | |
'oauth_timestamp': str(int(time.time())), | |
'oauth_nonce': unicode(csrf(request)['csrf_token']), | |
'oauth_version': '1.0', | |
'xoauth_lang_pref': 'en-us' | |
} | |
# create the consumer | |
consumer = oauth.Consumer(key=settings.YAHOO_CONSUMER_KEY, secret=settings.YAHOO_CONSUMER_SECRET) | |
# create the request | |
req = oauth.Request(method='GET', url=url, parameters=params) | |
# sign the request | |
signature_method = oauth.SignatureMethod_PLAINTEXT() | |
req.sign_request(signature_method, consumer, None) | |
# get the request token from yahoo | |
response = urllib2.urlopen(req.to_url()).read() | |
# parse the response | |
params = urlparse.parse_qs(response) | |
# store the returned values | |
request.session['yahoo_oauth_token'] = params['oauth_token'][0] | |
request.session['yahoo_oauth_token_secret'] = params['oauth_token_secret'][0] | |
# get the authorization URL | |
url = 'https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=' + params['oauth_token'][0] | |
return url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment