2

I am developing a Google Chrome Extension. I would like to be able to access the user profile info, using the data from these endpoints:

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

https://www.googleapis.com/oauth2/v1/userinfo

I don't seem to have access to these resources. From my web searches, it looks like I may need to enable a library API in the Google Developers Console. But I looked there and cannot find an appropriate API to enable. Does anyone know which API I should enable?

Community
  • 1
  • 1
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • In the case of use of ``https://www.googleapis.com/oauth2/v1/userinfo``, it is not required to enable API related to this. You can use the endpoint by access token retrieved from OAuth2 process. When you use this, please include ``https://www.googleapis.com/auth/userinfo.profile`` to the scopes. If this information was not useful for you, I'm sorry. – Tanaike Feb 05 '18 at 23:16
  • This is an additional information. As a test, I used it without enabling all APIs. The sample curl command is ``curl -H "Authorization: Bearer ### access token ###" "https://www.googleapis.com/oauth2/v1/userinfo"``. I confirmed that it worked. The scope of token is only ``https://www.googleapis.com/auth/userinfo.profile``. About the endpoint, ``https://www.googleapis.com/oauth2/v1/userinfo`` and ``https://www.googleapis.com/oauth2/v2/userinfo`` return the same information. – Tanaike Feb 05 '18 at 23:32
  • do you know what the difference is between the two urls in the OP? – Alexander Mills Feb 05 '18 at 23:41
  • About ``userinfo``, it returns the same information. Also v3 is the same. It seems that about the endpoints except for ``userinfo``, there is the difference between versions. https://stackoverflow.com/questions/31277898/difference-between-v1-v2-and-v3-in-https-www-googleapis-com-oauth2-v3-certs – Tanaike Feb 05 '18 at 23:54

3 Answers3

3

You will need to at least add the scope https://www.googleapis.com/auth/userinfo.profile as per mentioned.

Then you can use the oauth2 apis just like how would you use sheets, calendar etc.

In nodejs, it'd be something like this:

    const { google } = require('googleapis')
    const auth = new google.auth.OAuth2(
      keys.web.client_id,
      keys.web.client_secret,
      keys.web.redirect_uris[0]
    )
    // get the tokens from google
    auth.credentials = tokens
    const oauth2 = google.oauth2({ version: 'v2', auth })
    const userinfo = await oauth2.userinfo.get()
    console.log(userinfo)

The api is documented here: https://googleapis.dev/nodejs/googleapis/latest/oauth2/interfaces/Schema$Userinfo.html#info. There are also some examples in the source code there.

Ritankar Paul
  • 65
  • 1
  • 4
Ken H
  • 601
  • 8
  • 6
2

If you need just user's email-address for auth etc., you have to enable Google's Plus API, not the G**-*****d People API which is mentioned everywhere in the Google Cloud API docs.

Also, you need to mention the scope https://www.googleapis.com/auth/userinfo.email, not the https://www.googleapis.com/auth/userinfo.profile one.

p.s. It's an old question, yet unanswered. Possibly, you've already solved it. I'm posting it for people from the future, or just for my future self.

kmonsoor
  • 7,600
  • 7
  • 41
  • 55
  • 2
    You should know that Google+ related APIs are being shut down in January '19. More at [Google blog](https://www.blog.google/technology/safety-security/expediting-changes-google-plus/). – s3m3n Jan 16 '19 at 14:26
  • if google+ is become shutdown... wouldn't safe if we use gmail api? – gumuruh Sep 18 '19 at 02:11
2

You can retrieve data from these endpoints if you have an access token from the user. First, you have to get an access token by letting the user login using google login API. Then retrieve the data from the following link. https://www.googleapis.com/oauth2/v1/userinfo?access_token=PUT_YOUR_USER'S_ACCESS_TOKEN_HERE