How to call Dialogflow Rest API with OAuth access token

99封情书 提交于 2020-12-31 07:37:20

问题


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:


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



来源:https://stackoverflow.com/questions/62965088/how-to-call-dialogflow-rest-api-with-oauth-access-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!