0

I want to access the contacts of authenticated user . I have got the refresh and access token from oauth playground. How do I use these tokens to call people's api?

Community
  • 1
  • 1
  • Do you have the client id and the client secret? – ziganotschka Sep 04 '20 at 09:48
  • StackOverflow works best when you can show specific code that you are using, or a specific problem that you have been trying to solve, but that you need help in debugging the problem. Open-ended questions such as these are much more difficult. If you can show us what you've tried so far, or what,specifically, you're having problems with, we have a better chance of helping you. See [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Prisoner Sep 04 '20 at 10:00
  • Yes i do have a client id and secret @ziganotschka – Laxmi Anusri Patti Sep 04 '20 at 19:37

1 Answers1

2

In your case probably the easiest is to create an authentification flow as per documentation, whereby you hardcode the token.json file yourself.

  • If you run the sample code from the quickstart without having a token.json file, you will be redirected to an authorization URL where you have to authenticate with your account, so that a token file with your credentials will be created.
  • However, if you already have a token file for a user, you do not need to authorize the app by signing in as the given user.
  • The structure of a token.json file is the following:
{"access_token":"XXXXXXXXXXXXXXX","refresh_token":"YYYYYYYYYY","scope":"https://www.googleapis.com/auth/ZZZZZZZZZ","token_type":"Bearer","expiry_date":UNIX TIMESTAMP IN SECONDS}
  • If you possess the required information nothing stops you from creating a token.json file yourself

Alternatively

oauth2Client.setCredentials({
  refresh_token: 'STORED_REFRESH_TOKEN'
});

to assign to the oauth2Client a refresh token manually.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • I have followed the documentation to create an authentication flow, after authorizing the app , when i enter the code, I get the following error: { error: 'invalid_grant', error_description: 'Malformed auth code.' } – Laxmi Anusri Patti Sep 07 '20 at 19:45
  • 1
    Have you decoded the token? See [here](https://stackoverflow.com/a/54878799/11599789). There can be also other problems, e.g. user did not grant you offline access or revoked access, have a look at the [possible scenarios](https://stackoverflow.com/questions/10576386/invalid-grant-trying-to-get-oauth-token-from-google). – ziganotschka Sep 08 '20 at 06:39