Access google connections using google people API
Asked Answered
W

1

10

I would like to fetch all the google private connections of a user signed in from my app. I've enabled the Google People and the Google Plus API's. I set up the credentials API key, client id & client secret. The url with which I'm trying to fetch the users connections is

https://people.googleapis.com/v1/people/me/connections?fields=connections&key=api_key&access_token=access_token

Also, I'm using the library passport-google-oauth, to get the users access_token. Is there anything that I'm missing in the above URL.

My google auth code is

    // send to google to do the authentication
    // profile gets us their basic information including their name
    // email gets their emails
    app.get('/auth/google', passport.authenticate('google', {
        scope: ['profile', 'email','https://www.googleapis.com/auth/contacts.readonly', 'https://www.googleapis.com/auth/contacts']
    }));

    // the callback after google has authenticated the user
    app.get('/auth/google/callback',
    passport.authenticate('google', {
        successRedirect: '/profile',
        failureRedirect: '/'
    }));
Wore answered 3/3, 2017 at 19:42 Comment(0)
L
2

You have not mentioned what error you are getting but by looking at the url you are using I can tell you a few things.

people.connections.list access private user data. So for one you don't need to add Key that is just used for accessing public data. However having both should not result in any error message.

I have tested the request you are sending and it does work however this request requires that you have authenticated with at least one of the connections scopes.

https://www.googleapis.com/auth/contacts Requests that your app be given read and write access to the contacts in the authenticated user’s Google Contacts. https://www.googleapis.com/auth/contacts.readonly Requests that your app be given read access to the contacts in the authenticated user’s Google Contacts.

If you have not then you will get a no access error message.

{
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "status": "PERMISSION_DENIED"
  }
}
Latifundium answered 15/3, 2017 at 8:6 Comment(8)
Okay. I got the API key thing. But with the access_token I pass, I get the following error : Request is missing required authentication credential. Expected OAuth 2 access token google people api. I'm using the package google-passport-oauth to get the user token & google-api-nodejs-client to access people API for googleWore
@ParthVyas if you have got the access token, try using it as header of your request (perhaps Authorization: "Bearer " + accessToken)Append
@Append you don't need to do that by tacking &access_token= on the end of the request you are doing the same thing.Latifundium
@ParthVyas please post your auth code I think there is something wrong with your access token generationLatifundium
@DaImTo I've updated the question with the auth code, please have a look!Wore
@ParthVyas I have not tried the passport-google-oauth library, however according to github.com/google/…, IMO, you should have the authorization code to get an access tokenAppend
Yes @Append I'll try the google-api-nodejs-client now & check if the token is considered valid or notWore
@ParthVyas I am not a node dev but where are you adding the client id and client secret? c9.io/barberboy/passport-google-oauth2-exampleLatifundium

© 2022 - 2024 — McMap. All rights reserved.