Google Dialogflow API出现CORS问题。

huangapple go评论61阅读模式
英文:

Google Dialog flow API giving CORS issue

问题

以下是您要翻译的内容:

"I have created some intents in Google Dialogflow and trying to make a dialog flow API call 'detectIntent' in an Angular application. I'm using ApiAiClient of api-ai-javascript to make the call to Dialogflow. I also use OAuth 2.0 for authentication purposes. The authentication works fine, but when the Dialogflow API is called using the ApiAiClient, it responds with the below error.

Access to XMLHttpRequest at 'https://asia-south1-dialogflow.googleapis.com/****:detectIntentquery?v=20150910' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I used a Chrome plugin to suppress the CORS issue, but still I get the below error:

Access to XMLHttpRequest at 'https://asia-south1-dialogflow.googleapis.com/**:detectIntentquery?v=20150910' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

I want to make this API call to work without the Chrome extension. Is there any configuration on the Dialogflow API that I can do to fix the CORS issue? Should I be using an API gateway to add specific response headers to suppress CORS? I went through a lot of solutions in the forums but couldn't find an apt solution for this issue.

Following is the code in Angular which calls the Dialogflow:

import { Injectable } from '@angular/core';
import { environment } from './environments/environment';
import { ApiAiClient } from 'api-ai-javascript/es6/ApiAiClient';
import { ApiAiConstants, IApiClientOptions, IRequestOptions, IServerResponse } from 'api-ai-javascript';
import { Observable } from 'rxjs/internal/Observable';
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';

// Message class for displaying messages in the component

export class Message {

constructor(public content: string, public sentBy: string) {}

}

@Injectable()
export class ChatService {

  readonly token = environment.dialogflow.angularBot;
  readonly url = environment.dialogflow.url;
  readonly apiVersion = environment.dialogflow.version;

  options: IApiClientOptions = {
    accessToken: this.token, 
    baseUrl: this.url,
  }

  client = new ApiAiClient(this.options);

  conversation = new BehaviorSubject<Message[]>([]);
  constructor() {}

  // Sends and receives messages via DialogFlow

  converse(msg: string) {
      const userMessage = new Message(msg, 'user');
      this.update(userMessage);
      console.log(msg);
      return this.client.textRequest(msg).then((res) => {
        const speech = res.result.fulfillment.speech;
        const botMessage = new Message(speech, 'bot');
        this.update(botMessage); 
      }).catch(err => {
          console.log(err.message);
      })
  };
    // Adds message to source
    update(msg: Message) {
    this.conversation.next([msg]);
  }}

"

英文:

I have created some intents in Google Dialog flow and trying to make a dialog flow api call 'detectIntent' in an Angular application. I'm using ApiAiClient of api-ai-javascript to make the call to Dialog flow. I also use the oAuth2.0 for authentication purposes.
The Authentication works fine but when the dialog flow api is called using the ApiAiClient it responds with the below error.

Access to XMLHttpRequest at 'https://asia-south1-dialogflow.googleapis.com/****:detectIntentquery?v=20150910' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I used a chrome plugin to suppress the CORS issue but still I get the below error:
Access to XMLHttpRequest at 'https://asia-south1-dialogflow.googleapis.com/**:detectIntentquery?v=20150910' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

I want to make this Api call to work without the chrome extension, is there any configuration on the dialog flow api that I can do to fix the CORS issue? Should I be using a API gateway to add specific response headers to suppress CORS?
I went through a lot of solutions in the forums but couldn't find an apt solution for this issue.

Following is the code in angular which calls the dialog flow:

**import { Injectable } from &#39;@angular/core&#39;;
import { environment } from &#39;./environments/environment&#39;;
import { ApiAiClient } from &#39;api-ai-javascript/es6/ApiAiClient&#39;;
import { ApiAiConstants, IApiClientOptions, IRequestOptions,IServerResponse } from &#39;api-ai-javascript&#39;;
import { Observable } from &#39;rxjs/internal/Observable&#39;;
import { BehaviorSubject } from &#39;rxjs/internal/BehaviorSubject&#39;;
// Message class for displaying messages in the component
export class Message {
constructor(public content: string, public sentBy: string) {}
}
@Injectable()
export class ChatService {
readonly token = environment.dialogflow.angularBot;
readonly url = environment.dialogflow.url;
readonly apiVersion = environment.dialogflow.version;
options:IApiClientOptions = {
accessToken: this.token, 
baseUrl:this.url ,
}
client = new ApiAiClient(this.options);
conversation = new BehaviorSubject&lt;Message[]&gt;([]);
constructor() {}
// Sends and receives messages via DialogFlow
converse(msg:string) {
const userMessage = new Message(msg, &#39;user&#39;);
this.update(userMessage);
console.log(msg);
return this.client.textRequest(msg).then((res) =&gt; {
const speech = res.result.fulfillment.speech;
const botMessage = new Message(speech, &#39;bot&#39;);
this.update(botMessage); 
}).catch(err =&gt;{
console.log(err.message);
})
};
// Adds message to source
update(msg: Message) {
this.conversation.next([msg]);
}}**

答案1

得分: 1

你是否正在使用这个扩展? <br><br>另外,你可以尝试这个解决方案

> 从控制台启动 Chrome:
>
>chrome.exe --user-data-dir=&quot;C:/Chrome dev session&quot; --disable-web-security
>
>也许你需要关闭 Chrome 中的所有标签,然后重新启动它。

英文:

Are you using this extension? <br><br>Also can you try this solution:

> Start Chrome from the Console:
>
>chrome.exe --user-data-dir=&quot;C:/Chrome dev session&quot; --disable-web-security
>
>Maybe you have to close all Tabs in Chrome and restart it.

huangapple
  • 本文由 发表于 2023年6月19日 19:05:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定