Created
June 10, 2017 20:28
-
-
Save cosbgn/603a3f73882b3fb30c9f4aaf0652efb5 to your computer and use it in GitHub Desktop.
Example of Google oAuth2Client login
This file contains hidden or 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
# /start | |
def start_google(request): | |
"""first step Oauth2 to google analytics and get code""" | |
flow = OAuth2WebServerFlow(client_id = settings.GOOGLE_OAUTH2_CLIENT_ID, | |
client_secret = settings.GOOGLE_OAUTH2_CLIENT_SECRET, | |
scope = settings.GOOGLE_SCOPE, | |
redirect_uri = settings.OAUTH_REDIRECT_URI, | |
access_type='offline', | |
) | |
auth_uri = flow.step1_get_authorize_url() | |
return HttpResponseRedirect(auth_uri) # This will then redirect back to /gconnect after the user accepted to share his/her data. | |
# /gconnect | |
def exchange_code_and_login(request): | |
"""Step 2: connect to google analytics API""" | |
flow = OAuth2WebServerFlow(client_id = settings.GOOGLE_OAUTH2_CLIENT_ID, | |
client_secret = settings.GOOGLE_OAUTH2_CLIENT_SECRET, | |
scope = settings.GOOGLE_SCOPE, | |
redirect_uri = settings.OAUTH_REDIRECT_URI, | |
access_type='offline', # Does it work? How can I test it? | |
) | |
if request.method == 'GET': | |
if 'code' in request.GET: | |
code = request.GET['code'] | |
credentials = flow.step2_exchange(code) | |
http = httplib2.Http() | |
http = credentials.authorize(http) | |
v3_service = build('analytics', 'v3', http=http) | |
google_profile = v3_service.management().accountSummaries().list().execute() | |
google_email = google_profile['username'] | |
# Check if user exists | |
try: | |
current_user = User.objects.get(username=google_email) | |
except User.DoesNotExist: | |
current_user = User.objects.create_user(username=google_email) | |
current_user.backend = 'django.contrib.auth.backends.ModelBackend' | |
# Save Google Profile to DB | |
obj, created = GoogleProfile.objects.get_or_create(user_id=current_user.id, defaults={'google_profile': google_profile,'google_email': current_user,}) | |
if not created: # If the user exists, these things will be replaced: | |
obj.google_profile = google_profile | |
obj.google_email = google_email | |
obj.save() | |
# Save Credentials to DB | |
obj, created = CredentialsModel.objects.get_or_create(user_id=current_user.id, defaults={'credentials': credentials, 'google_email': google_email}) | |
if not created: # If the user exists, these things will be replaced: | |
obj.credentials = credentials | |
obj.google_email = google_email | |
obj.save() | |
# Login User | |
if current_user: | |
login(request, current_user) | |
else: | |
print('problem logging in user') | |
current_user = None | |
return user_settings(request) # Or any view, the user is now logged in and credentials are save in the DB | |
else: | |
return HttpResponse('Sorry, code not in request. <br> <br> Click <a href="/start"> here</a> to try again') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment