2

My web app is successfully going through google's recommended flow to get credentials to query Google Drive API. This works fine. However, when I try to use the same credentials already obtained to get the user's email and name, I get an error.

Here I retrieve credentials and query Google Drive API. This works perfectly fine

def analyze():
 credentials = getCredentials() 
 drive_service = googleapiclient.discovery.build('drive', 'v3', credentials=credentials)
 theFiles = drive_service.files().list(pageSize=1000,q="trashed=false", fields="files(id,name,modifiedTime, size)").execute() #THIS WORKS

Right after that, I try to use the SAME CREDENTIALS to get user info, but now it doesn't work

oauth2_client = googleapiclient.discovery.build('oauth2','v2',credentials=credentials)
 user_info= oauth2_client.userinfo().get().execute() #THIS FAILS
givenName = user_info['given_name']

Error: https://www.googleapis.com/oauth2/v2/userinfo?alt=json returned "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.">

SOME OTHER IMPORTANT FUNCTIONS:

def getCredentials():
 *Loads credentials from the session.*
 sc = session['credentials'] 
 credentials = google.oauth2.credentials.Credentials(token=sc.get('token'),
 client_id=sc.get('client_id'),
 refresh_token=sc.get('refresh_token'),
 token_uri=sc.get('token_uri'),
 client_secret=sc.get('client_secret'),
 scopes=sc.get('scopes'))

the credentials are obtained in the callback page:

@app.route('/OAcallback')
def OAcallback():
   flow =google_auth_oauthlib.flow.Flow.from_client_secrets_file('client_id.json', scopes=['https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.profile'])
  flow.redirect_uri = return_uri
  authorization_response = request.url
  flow.fetch_token(authorization_response=authorization_response)
  credentials = flow.credentials 
  * Store the credentials in the session.*
  credentials_to_dict(credentials)

Please help me understand why my credentials are not working when trying to get user info. What should I change?

Thanks in advance!!!

1 Answer 1

0

You are only requesting the profile scope. To also request the email address add the scope email.

Change this part of your code from:

scopes=['https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.profile']

to:

scopes=['https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.profile' 'email']
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I changed my code to add email, but I am still getting the exact same error: <HttpError 401 when requesting googleapis.com/oauth2/v2/userinfo?alt=json returned "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See developers.google.com/identity/sign-in/web/devconsole-project.">
1) I do not see where you are storing your credentials in the session. Post complete code and not fragments. 2) Where in your code are you getting this error? 3) Show both the page where you authenticate the user and the page where you process the redirect.
One additional point. Once you changed the scopes in your code, you need to invalidate the old sessions.
Thank you @JohnHanley !!! I'm so stupid. The problem was originated by the fact I wasn't invalidating the old sessions. Once I corrected that, the program runs perfectly. I couldn't have discovered it without your help!
@ClementBjelland - You are welcome, glad to help. Don't forget to close this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.