Search code examples
node.jsdialogflow-esactions-on-googlegoogle-authenticationdialogflow-es-fulfillment

How to use refresh and access token obtianed using Oauth playground to call People API in my Google Action?


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?


Solution

  • 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.