Skip to content

Instantly share code, notes, and snippets.

@chriskief

chriskief/google.py

Created Oct 24, 2013
Embed
What would you like to do?
import urllib
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 = urllib.quote_plus(settings.SITE_URL + reverse('register_google'))
# create a unique state value for CSRF validation
request.session['google_state'] = unicode(csrf(request)['csrf_token'])
scope = urllib.quote_plus('https://www.googleapis.com/auth/userinfo.email') + '+' + \
urllib.quote_plus('https://www.googleapis.com/auth/userinfo.profile')
# redirect to google for approval
url = 'https://accounts.google.com/o/oauth2/auth?' \
+ 'scope=' + scope \
+ '&state=' + request.session['google_state'] \
+ '&redirect_uri=' + redirect_url \
+ '&response_type=code' \
+ '&client_id=' + settings.GOOGLE_OAUTH2_CLIENT_ID \
+ '&access_type=offline' \
+ '&approval_prompt=auto'
return url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment