如何使用OAuth访问令牌调用Dialogflow Rest API



我已经在谷歌控制台中创建了项目启用对话流API已创建OAuth v2凭据使用这个凭证,我调用了访问令牌api来生成令牌

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**

我收到了访问令牌,并将其传递给对话流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"
}
}
}

我仍然得到这个错误

"错误":{"代码":401,"消息":"请求的身份验证凭据无效。应为OAuth 2访问令牌、登录cookie或其他有效身份验证凭据。请参阅https://developers.google.com/identity/sign-in/web/devconsole-project.",…}

我无法识别哪里出了问题

请帮忙提前感谢

我在api中传递的代码是OAuth代码(感谢John Hanley(

API从OAuth代码生成访问令牌

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"
}

作为回应,您收到此

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

在谷歌API 的头部传递此访问令牌

最新更新