我在离子3中使用对话流程。我试图通过http请求发送并获得响应。这是我的home.ts文件代码。
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Message } from '../module/message.module';
import { ChatProvider } from '../../providers/chat/chat';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
messages: Message[] = [];
constructor(public navCtrl: NavController,private chatprovider: ChatProvider) {
}
// ionViewDidLoad(){
// let message = new Message("hi")
// this.messages.push(message);
// }
sendMesage(message_content) {
this.chatprovider.sendMessage(message_content.value).subscribe(res =>{
console.log(res);
// let message = new Message(res.result.speech);
// this.messages.push(message);
// message_content.value = '';
}, err => {
console.log(err)
}
)
}
}
这是我的chatprovider.ts文件代码
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
@Injectable()
export class ChatProvider {
url = 'https://api.dialogflow.com/v1/query';
accessToken = 'xxxxxxxxxxxxxxxxxxxxxxxx';
constructor(public http: HttpClient) {
console.log('Hello ChatProvider Provider');
}
sendMessage(message: string) {
let data = {
lang: "en",
query: message
}
let headers = new HttpHeaders();
headers.append("Authorization","Bearer" + this.accessToken);
return this.http.post(this.url, data,{headers: headers})
.map(res => {
return res
})
}
}
这是我面临的错误
polyfills.js:3 POST https://api.dialogflow.com/v1/query 403()s @ polyfills.js:3 t.scheduleTask @polyfills.js:3 onScheduleTask @ polyfills.js:3 t.scheduleTask @polyfills.js:3 r.scheduleTask @ polyfills.js:3 r.scheduleMacroTask @polyfills.js:3(匿名)@ polyfills.js:3 o。(匿名函数)@polyfills.js:2(匿名)@ http.js:2364 Observable._trySubscribe @ Observable.js:172 Observable.subscribe @ Observable.js:160 subscribeToResult @ subscribeToResult.js:23 MergeMapSubscriber._innerSub @ mergeMap.js:132 MergeMapSubscriber._tryNext @ mergeMap.js:129 MergeMapSubscriber._next @ mergeMap.js:112 Subscriber.next @ Subscriber.js:93 ScalarObservable._subscribe @ ScalarObservable.js:49 Observable._trySubscribe @ Observable.js:172 Observable.subscribe @ Observable.js:160 MergeMapOperator.call @ mergeMap.js:87 Observable.subscribe @ Observable.js:157 FilterOperator.call @ filter.js:60 Observable.subscribe @ Observable.js:157 MapOperator.call @ map.js:56 Observable.subscribe @ Observable.js:157 MapOperator.call @ map.js:56 Observable.subscribe @ Observable.js:157 webpackJsonp.197.HomePage.sendMesage @ home.ts:23(匿名)@ HomePage.html:24 handleEvent @ core.js:13589 callWithDebugContext @ core.js:15098 debugHandleEvent @ core.js:14685 dispatchEvent @ core.js:10004(匿名)@ core.js:10629(匿名)@ platform-browser.js:2628 t.invokeTask @polyfills.js:3 onInvokeTask @ core.js:4751 t.invokeTask @polyfills.js:3 r.runTask @polyfills.js:3 e.invokeTask @polyfills.js:3 p @polyfills.js:2 v @ polyfills.js:2 home.ts:29