Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Dialogflow Rest API with OAuth access token

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

like image 344
PSD Avatar asked Nov 30 '25 08:11

PSD


1 Answers

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

like image 190
PSD Avatar answered Dec 02 '25 06:12

PSD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!