3
votes

I have created project in google console Enable the Dialogflow API Created OAuth v2 credential Using this credentials i called access token api to generate token

https://accounts.google.com/o/oauth2/v2/auth?
 scope=https://www.googleapis.com/auth/dialogflow&
 access_type=offline&
 include_granted_scopes=true&
 response_type=code&
 state=state_parameter_passthrough_value&
 redirect_uri=http://localhost&
 client_id= **i placed client id here**

I received access token and passed it to Dialog flow API

https://dialogflow.googleapis.com/v2/projects/**PROJECT-ID**/agent/sessions/123456:detectIntent
Header
Content-Type : application/json; charset=utf-8
Authorization : Bearer **ACCESS_TOKEN**

Body
{
  "query_input": {
    "text": {
      "text": "I know french",
      "language_code": "en-US"
    }
  }
}

Still i am getting this error

"error":{"code": 401, "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",…}

i am not able to identify where i went wrong

Please help thanks in advance

1
Your example is requesting an OAuth Code (which is not an Access Token). You then need to exchange the code for the OAuth tokens.John Hanley
Thanks for reply John, How to get OAuth token from that codePSD
Hey John Hanley thanks, you were right, i figured out how to generate access token from OAuth codePSD
Thank you for posting an answer.John Hanley

1 Answers

3
votes

The code that i was passing in api was the OAuth Code(Thanks John Hanley)

API to generate Access token from OAuth Code

Post : https://oauth2.googleapis.com/token
Content-Type: application/x-www-form-urlencoded
{
  "code":"OAuth Code",
  "client_id":"Client ID",
  "client_secret":"Client Secret",
  "redirect_uri":"http://localhost",
  "grant_type":"authorization_code"
}

In response you receive this

Response
{
"access_token": "Token",
"expires_in": 3599,
"refresh_token": "Refresh Token",
"scope": "https://www.googleapis.com/auth/dialogflow",
"token_type": "Bearer"
}

Pass this access token in header of google API