Skip to content

Instantly share code, notes, and snippets.

@chriskief
Created October 24, 2013 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriskief/7130950 to your computer and use it in GitHub Desktop.
Save chriskief/7130950 to your computer and use it in GitHub Desktop.
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