Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ud330/Lesson2/step5/project.py Google Log In doesn't work #74

Open
alberto139 opened this issue Nov 7, 2018 · 6 comments
Open

ud330/Lesson2/step5/project.py Google Log In doesn't work #74

alberto139 opened this issue Nov 7, 2018 · 6 comments

Comments

@alberto139
Copy link

Hi,

After updating the client id and client_secrets.json I've found that the Google sign in still doesn't work.
I've seen other issues suggesting fixes to this but no one from Udacity seems to have responded to any of them.

The issue that I'm experiencing is that once the Sign in button is clicked and the google acount is chosen from the pop-up window, the window closes and nothing else happens. It seems to me that the signInCallback function in login.html is not actually being called.

@alberto139
Copy link
Author

I've found that the problem might lie with my browser (Google Chrome). I tried on Firefox and it works correctly.

I suggest using something like flask-dance for future versions of this lesson.

@W2AlharbiMe
Copy link

I've issues in redirect_uri also after searching i found that oauth2client is now deprecated 🤔
i didn't found any good and up to date example on how to add google login

@somphouang
Copy link

Here is what works for me. You need to configure the Google API Client key as well for this to work at https://console.developers.google.com/apis/

import google_auth_oauthlib.flow
import googleapiclient.discovery

Allow Google GConnect

PROJECT_DIR = os.path.dirname(file)
CLIENT_SECRETS_FILE = os.path.join(PROJECT_DIR, 'client_secrets.json')
SCOPES = ['https://www.googleapis.com/auth/userinfo.profile']

@app.route('/login', methods=['GET'])
def login_route():
"""
Redirects user to Google auth link
"""
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE, scopes=SCOPES)
flow.redirect_uri = url_for('oauth2callback', _external=True)

authorization_url, state = flow.authorization_url(
    access_type='offline',
    include_granted_scopes='true')

login_session['state'] = state

return redirect(authorization_url)

@app.route('/logout', methods=['GET'])
def logout_route():
"""
Cleansup credentials from session and
as result user logged out
"""
if 'credentials' in login_session:
del login_session['credentials']
flash('You logged out')
return redirect(url_for('index_route'))

@app.route('/oauth2callback')
def oauth2callback():
"""
Handles callback call from google, and finished authorization,
then retrieves user info and saves it into session till next login
"""
# Specify the state when creating the flow in the callback so that it can
# verified in the authorization server response.
state = login_session['state']

flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
    CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
flow.redirect_uri = url_for('oauth2callback', _external=True)

# Use the authorization server's response to fetch the OAuth 2.0 tokens.
authorization_response = request.url

flow.fetch_token(authorization_response=authorization_response)

# Store credentials in the session.
# ACTION ITEM: In a production app, you likely want to save these
#              credentials in a persistent database instead.
credentials = flow.credentials
login_session['credentials'] = credentials_to_dict(credentials)

# requesting user info
service = googleapiclient.discovery.build('people', 'v1',
                                          credentials=credentials)
result = service.people().get(resourceName='people/me',
                              personFields='names,photos').execute()

user_id = result['resourceName']
user_name = result['names'][0]['displayName']
user_photo = url_for('static', filename='images/no-profile-photo.svg')
if len(result['photos']) > 0:
    user_photo = result['photos'][0]['url']

# saving user info to session so it remains persistent
# this updates on login process
login_session['user_id'] = user_id
login_session['user_name'] = user_name
login_session['user_photo'] = user_photo

return redirect(url_for('index_route'))

@mchesler613
Copy link

When I'm on Firefox, Facebook Login doesn't work.
When I'm on Chrome, Google Login doesn't work.

@somphouang
Copy link

Make sure you guys use it with the API keys you get yourself from Facebook and Google Dev

@jz903
Copy link

jz903 commented Dec 12, 2019

According to Migrate from Google+ sign-in

The Google+ Sign-in feature has been fully deprecated as of March 7, 2019.
Developers should migrate to the more comprehensive Google Sign-in authentication system.
Migration tips are also available for Android.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants