Google OAuth API to get user's email address?

前端 未结 11 851
栀梦
栀梦 2020-11-29 01:38

I am playing with Google\'s OAuth 2.0 Playground using my own personal Google account, but I cannot seem to recover my Gmail address using the playground.

The scope

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 01:46

    To retrieve the email address, you need to include the scope: "https://www.googleapis.com/auth/userinfo.email" as mentioned in this document. If this scope is included while you generate the refresh token, you should be able to get the email address of the authenticating user by making the following request:

    you can call this with your own access token then will give the response

    https://www.googleapis.com/oauth2/v3/userinfo?access_token="YOUR_ACCESS_TOKEN"

    response will look like this

    {
      "sub": "1057abc98136861333615xz",
      "name": "My Name",
      "given_name": "My",
      "family_name": "Name",
      "picture": "https://lh3.googleusercontent.com/a-/AOh14qiJarwP9rRw7IzxO40anYi4pTTAU_xseuRPFeeYFg",
      "email": "MyName@gmail.com",
      "email_verified": true,
      "locale": "en"
    }
    

    or simply you can just write a function

    import requests
    def get_user_email(access_token):
        r = requests.get(
                'https://www.googleapis.com/oauth2/v3/userinfo',
                params={'access_token': access_token})
        return r.json()
    

提交回复
热议问题