Search code examples
angulartypescripthttpdialogflow-es

How to fix Authentication error with Dialogflow V2 query


I want to query Dialogflow V2 from an Angular application but the Authentication header doesn't seem to work.

I always get error code 401 when I run this code:

readonly baseURL: string = "https://dialogflow.googleapis.com/v2/projects/myprojectname/agent/sessions/12345:detectIntent";

readonly token = environment.dialogflow.token;

constructor(private http: HttpClient) {}

talk(query: string) {
    let body = {
      "queryInput": {
        "text": {
          "text": query,
          "languageCode": "en"
        }
      }
    }


    this.http.post(this.baseURL, body, {headers: {'Authorization': `Bearer 
     ${this.token}`}}).subscribe(
      res => {
        return console.log(res);
      }
    );
  }

What have I done wrong?


Solution

  • I figured it out myself now: I was using token from Dialogflow v1, so of course it didn't work.