You are not authorized for this operation. Invalid access token DialogFlow v2

痴心易碎 提交于 2019-12-24 07:07:16

问题


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:

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

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!

Edit:

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?\""
          }
        ]
      }
    ]
  }
}

回答1:


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.

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.

Update based on your code once you've moved it to V2:

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.

So your body parameter might be setup something like this:

var body = {
  queryInput: {
    text: {
      text: "Hello",
      language: "en-US"
    }
  }
};
var bodyStr = JSON.stringify(body);

and then set in your request() options.




回答2:


Because you have put wrong URL in your project.
Open below image and see which URL use for posturl in your project

https://i.stack.imgur.com/3ym9n.png



来源:https://stackoverflow.com/questions/50638542/you-are-not-authorized-for-this-operation-invalid-access-token-dialogflow-v2

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