Google OAuth userinfo method doesn't return email address

Viewed 3508

I'm using Google APIs with Postman and I'm using to retrieve my token and it works.

https://accounts.google.com/o/oauth2/token?code={my_code}

enter image description here

Now, with access_token I would like to have the user data so I'm calling this method:

https://www.googleapis.com/oauth2/v3/userinfo?access_token={my_access_token}

But my response is

enter image description here

I already tryied with userinfo.email but resonse is not found enter image description here

Is there any way to get the user email? Thanks


update: tryied also setting scope to auth call enter image description here

and userinfo call enter image description here

But no email returned.

3 Answers

From the documentation:

email is provided only if your scope included the email scope value.

To do that, set an additional scope to your application:

https://www.googleapis.com/auth/userinfo.email

You can go to Developer's Console then add the scope by customizing your user consent screen. The goal here is you need to see the userinfo.email along with userinfo.profile scope whenever you retrieve your access token using Postman.

Another way is you have to generate another authorization code again. When authorizing, you will be asked to include a scope on your request and there you include the two scopes that you need.

The userinfo.endpoint is not designed or guaranteed to return the claimed every time. If you want to get the users email address every time and have included the email scope you should be going though the People api.

People.get

curl \
  'https://people.googleapis.com/v1/people/me?personFields=emailAddresses' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

response

{
  "resourceName": "people/117200475532672775346",
  "etag": "%EgUBCT43LhoEAQIFByIMR3BzQkR2cnI1ZGc9",
  "emailAddresses": [
    {
      "metadata": {
        "primary": true,
        "verified": true,
        "source": {
          "type": "ACCOUNT",
          "id": "117200475532672775346"
        }
      },
      "value": "xxxx@gmail.com"
    },
    {
      "metadata": {
        "verified": true,
        "source": {
          "type": "PROFILE",
          "id": "117200475532672775346"
        }
      },
      "value": "xxxx@gmail.com",
      "type": "home",
      "formattedType": "Home"
    },
    {
      "metadata": {
        "source": {
          "type": "CONTACT",
          "id": "3faa96eb08baa4be"
        }
      },
      "value": "xxxx@gmail.com",
      "type": "home",
      "formattedType": "Home"
    },
    {
      "metadata": {
        "source": {
          "type": "CONTACT",
          "id": "3faa96eb08baa4be"
        }
      },
      "value": "xxxx@hotmail.com",
      "type": "other",
      "formattedType": "Other"
    },
    {
      "metadata": {
        "source": {
          "type": "CONTACT",
          "id": "3faa96eb08baa4be"
        }
      },
      "value": "xxxx@daimto.com",
      "type": "other",
      "formattedType": "Other"
    },
    {
      "metadata": {
        "source": {
          "type": "CONTACT",
          "id": "3faa96eb08baa4be"
        }
      },
      "value": "xxxx@eg.dk",
      "type": "work",
      "formattedType": "Work"
    }
  ]
}

beyond that i think you should consider using the auth tab in postman and selecting, oauth2 then you should be able to add the client id and everything there. Which is much easier then swapping out all the codes yourself.

Related