您无权执行此操作。无效的访问令牌DialogFlow v2 [英] You are not authorized for this operation. Invalid access token DialogFlow v2
问题描述
我正在尝试使用DialogFlow v2端点,但是由于某种原因我没有得到授权消息,因此我可以使用以下命令生成访问令牌:
I am trying to use DialogFlow v2 endpoint but for some reason I am getting not authorized message, eventhou I am able to generate access token using the following command:
最初,我正在运行此命令来为我的本地计算机授权该服务,以便能够对该服务进行授权: gcloud auth activate-service-account --key-file =< service-account-key-file .json>
,然后通过以下命令获取访问令牌: gcloud auth print-access-token
和此访问令牌,我将其附加在以下代码上:
Initially I am running this to authorize the service for my local machine to be able to authorize to the service: gcloud auth activate-service-account --key-file=<service-account-key-file.json>
then I get access token by following command: gcloud auth print-access-token
and this access token I am attaching on the following code:
fetch(configs.baseUrl + "query?v=20150910", {
body: JSON.stringify({query: text, lang: "en", sessionId: "somerandomthing"}),
headers: {
'content-type': 'application/json',
"Authorization": "Bearer " + accessToken,
},
method: 'POST',
})
.then(response => response.json())
.then(data => {
console.log(data.result.fulfillment.speech);
return data.result.fulfillment.speech;
})
.catch(error => console.error(error))
我不知道这是与DialogFlow V2进行通讯的正确方法吗?
请您让我知道我在做什么错,为什么它说我没有被授权,因为我已经通过上述命令进行了授权并且能够获得访问令牌!
I dont know if this is the right way to achieve a communication with DialogFlow V2? Please if you could let me know what I am doing wrong and why I it says I am not authorised since I am authorizing by the above commands and been able to get access token!
编辑:
经过几处更改,我的代码最终看起来像这样:
After few changes my code finally looks like this:
fetch("https://dialogflow.googleapis.com/v2beta1/projects/xxx/agent/sessions/xxx/:detectIntent", {
body: JSON.stringify({queryInput: "Hello"}),
headers: {
'content-type': 'application/json',
"Authorization": "Bearer xxxx",
},
method: 'POST',
})
.then(response => response.json())
.then(data => {
console.log(data.result.fulfillment.speech);
return data.result.fulfillment.speech;
})
.catch(error => console.error(error))
我收到的新错误消息是:
and the new error message I get is:
{
"error": {
"code": 400,
"message": "Invalid value at 'query_input' (type.googleapis.com/google.cloud.dialogflow.v2beta1.QueryInput), \"Hello\"",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "query_input",
"description": "Invalid value at 'query_input' (type.googleapis.com/google.cloud.dialogflow.v2beta1.QueryInput), \"Saanko yhteystiedot?\""
}
]
}
]
}
}
推荐答案
您没有显示正在使用的 baseUrl
,但这看起来像V1 API而不是V2 API。您应该将代码迁移到V2 。
You don't show the baseUrl
you're using, but that looks like the V1 API rather than the V2 API. You should migrate your code to V2.
还要记住,访问令牌已过期,因此您需要定期生成一个新令牌。您无法请求长期令牌(这被认为是不安全的),但是应该让您的代码调用 gcloud auth print-access-token
(或使用库来执行
Keep in mind, also, that the access token expires, so you will need to generate a new one periodically. You cannot request a "long lived" token (this is considered insecure), but should have your code call gcloud auth print-access-token
(or use a library to do the same thing) before the previous one expires.
更新,将其移至V2之后:
Update based on your code once you've moved it to V2:
queryInput
参数不直接使用字符串。必须将其设置为 QueryInput对象。这是一个枚举,因此只能设置一个指定的字段。看来您想要文本
字段,该字段需要 TextInput对象。
The queryInput
parameter doesn't take a string directly. It must be set to a QueryInput object. This is an enum, so can only have one of the fields specified set. It looks like you want the text
field which requires a TextInput object.
所以您的 body
参数可能可以这样设置:
So your body
parameter might be setup something like this:
var body = {
queryInput: {
text: {
text: "Hello",
language: "en-US"
}
}
};
var bodyStr = JSON.stringify(body);
,然后在您的 request()
中进行设置选项。
and then set in your request()
options.
这篇关于您无权执行此操作。无效的访问令牌DialogFlow v2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!