如何使用OAuth访问令牌调用Dialogflow Rest API [英] How to call Dialogflow Rest API with OAuth access token
问题描述
我已经在Google控制台中创建了项目启用Dialogflow API创建了OAuth v2凭证使用此凭据,我调用了访问令牌api来生成令牌
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**
我收到了访问令牌并将其传递给Dialog flow API
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"
}
}
}
仍然出现此错误
错误":{代码":401,消息":请求具有无效的身份验证凭据".预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据.请参阅 https://developers.google.com/identity/sign-in/web/devconsole-project.",...}
"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
请提前帮助
推荐答案
我在api中传递的代码是OAuth代码(感谢John Hanley)
The code that i was passing in api was the OAuth Code(Thanks John Hanley)
API可从OAuth代码生成访问令牌
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"
}
在Google API的标头中传递此访问令牌
Pass this access token in header of google API
这篇关于如何使用OAuth访问令牌调用Dialogflow Rest API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!