Javascript 如何使用简单的ajax jQuery对DialogFlow V2进行http调用?
在使用simply jquery之前,我一直在使用DialogFlow v1,它的工作非常顺利 现在,我必须切换到V2,我被困在如何保持相同的代码,但只是用V2修改 我一直在查看V2的这个客户端库: 但是我不想使用Node.js,我只是不想做Node server.js之类的事情来运行应用程序,而且我也不确定我是否可以将jQuery与Node.js混合使用 我以前的代码v1如下所示:Javascript 如何使用简单的ajax jQuery对DialogFlow V2进行http调用?,javascript,google-api,google-cloud-platform,google-api-client,dialogflow-es,Javascript,Google Api,Google Cloud Platform,Google Api Client,Dialogflow Es,在使用simply jquery之前,我一直在使用DialogFlow v1,它的工作非常顺利 现在,我必须切换到V2,我被困在如何保持相同的代码,但只是用V2修改 我一直在查看V2的这个客户端库: 但是我不想使用Node.js,我只是不想做Node server.js之类的事情来运行应用程序,而且我也不确定我是否可以将jQuery与Node.js混合使用 我以前的代码v1如下所示: fetch(url, { body: JSON.stringify(data), // cac
fetch(url, {
body: JSON.stringify(data),
// cache: 'no-cache',
// credentials: 'same-origin',
headers: {
'content-type': 'application/json',
"Authorization": "Bearer " + configs.accessToken,
},
method: 'POST',
mode: 'cors',
redirect: 'follow',
referrer: 'no-referrer',
})
.then(response => response.json()) // parses response to JSON
我切换到ES6对dialogflow进行http请求,但我希望在V2中使用相同的代码,这可能吗?另外,我无法再看到v2的访问令牌,我们应该如何处理http调用的身份验证
我真的很困惑新的V2,因为我们切换到企业版帐户,这是我们必须使用V2,它有点糟糕
编辑:
我正在从文档中检查此示例:
POST https://dialogflow.googleapis.com/v2beta1/projects/project-name/agent/intents
Headers:
Authorization: Bearer $(gcloud auth print-access-token)
Content-Type: application/json
POST body:
{
'displayName': 'StartStopwatch',
'priority': 500000,
'mlEnabled': true,
'trainingPhrases': [
{
'type': 'EXAMPLE',
'parts': [
{
'text': 'start stopwatch'
}
]
}
],
'action': 'start',
'messages': [
{
'text': {
'text': [
'Stopwatch started'
]
}
}
],
}
但我不知怎么搞糊涂了:Authorization:Bearer$(gcloud auth print access token)
从哪里获得访问令牌
我已经完成了这一部分:gcloud auth activate service account--key file=我不知道激活后它在做什么!我希望我能从中得到一些访问令牌,但似乎没有什么只是一条消息,上面写着激活的服务…要将Dialogflow V2 API与浏览器AJAX一起使用,就像V1一样,没有简单的方法,除非您有访问令牌。我也遇到过同样的问题,我发现如果不使用他们的客户机库(SDK)或“GoogleOAuthJWT”,这是不可能做到的。在我的示例中,我使用了nodejs-googleoauthjwt包,它为用于浏览器AJAX调用的应用程序提供“访问令牌”。您不必使用他们的NodeJSSDK库,以防您在客户端处理逻辑 安装说明: 1.在dialogflow帐户上从V1配置V2 API,请遵循迁移指南。下载具有唯一电子邮件和键值的JSON文件。您可能希望通过注册域来授予对应用程序的访问权限 2.创建一个nodejs应用程序并使用“google oauth jwt”获取访问令牌。另外,将其作为一个服务,以便在进行任何ajax调用之前先调用它,以准备好访问令牌。以下是示例代码:
app.get("/your_sample_web_service_to_get_access_token", (req, res, next) => {
new Promise((resolve) => {
tokens.get({
//find this email value from the downloaded json
email: 'xxx@xxx.iam.gserviceaccount.com',
//find this key value from the downloaded json
key: '-----BEGIN PRIVATE KEY-----xxx',
//specify the scopes you wish to access: as mentioned in dialogflow documentation
scopes: ['https://www.googleapis.com/auth/cloud-platform']
},
(err, token) => {
//rest api response
res.json({
"access_token": token
});
resolve(token);
}
);
});
});
app.service('chatbot', function ($http, $rootScope) {
this.callAPI = function (user_entered_query) {
//I used detectintent REST API endpoint: find the project name from your account.
var endpoint = "https://dialogflow.googleapis.com/v2/projects/xxx/agent/sessions/123456789:detectIntent";
var data = JSON.stringify({queryParams:{}, query_input:{text:{text:user_entered_query,language_code:"en-US"}},outputAudioConfig:{},inputAudio:""});
var headers = {
//use the token from nodejs service
"Authorization": "Bearer " +$rootScope.token
};
return $http.post(_url, _data, {"headers": headers});
}
});
3.在客户端JavaScript中,使用从上述nodejs应用程序获得的访问令牌进行AJAX调用。以下是示例代码:
app.get("/your_sample_web_service_to_get_access_token", (req, res, next) => {
new Promise((resolve) => {
tokens.get({
//find this email value from the downloaded json
email: 'xxx@xxx.iam.gserviceaccount.com',
//find this key value from the downloaded json
key: '-----BEGIN PRIVATE KEY-----xxx',
//specify the scopes you wish to access: as mentioned in dialogflow documentation
scopes: ['https://www.googleapis.com/auth/cloud-platform']
},
(err, token) => {
//rest api response
res.json({
"access_token": token
});
resolve(token);
}
);
});
});
app.service('chatbot', function ($http, $rootScope) {
this.callAPI = function (user_entered_query) {
//I used detectintent REST API endpoint: find the project name from your account.
var endpoint = "https://dialogflow.googleapis.com/v2/projects/xxx/agent/sessions/123456789:detectIntent";
var data = JSON.stringify({queryParams:{}, query_input:{text:{text:user_entered_query,language_code:"en-US"}},outputAudioConfig:{},inputAudio:""});
var headers = {
//use the token from nodejs service
"Authorization": "Bearer " +$rootScope.token
};
return $http.post(_url, _data, {"headers": headers});
}
});
要像V1一样将Dialogflow V2 API与浏览器AJAX一起使用,没有简单的方法,除非您拥有访问令牌。我也遇到过同样的问题,我发现如果不使用他们的客户机库(SDK)或“GoogleOAuthJWT”,这是不可能做到的。在我的示例中,我使用了nodejs-googleoauthjwt包,它为用于浏览器AJAX调用的应用程序提供“访问令牌”。您不必使用他们的NodeJSSDK库,以防您在客户端处理逻辑 安装说明: 1.在dialogflow帐户上从V1配置V2 API,请遵循迁移指南。下载具有唯一电子邮件和键值的JSON文件。您可能希望通过注册域来授予对应用程序的访问权限 2.创建一个nodejs应用程序并使用“google oauth jwt”获取访问令牌。另外,将其作为一个服务,以便在进行任何ajax调用之前先调用它,以准备好访问令牌。以下是示例代码:
app.get("/your_sample_web_service_to_get_access_token", (req, res, next) => {
new Promise((resolve) => {
tokens.get({
//find this email value from the downloaded json
email: 'xxx@xxx.iam.gserviceaccount.com',
//find this key value from the downloaded json
key: '-----BEGIN PRIVATE KEY-----xxx',
//specify the scopes you wish to access: as mentioned in dialogflow documentation
scopes: ['https://www.googleapis.com/auth/cloud-platform']
},
(err, token) => {
//rest api response
res.json({
"access_token": token
});
resolve(token);
}
);
});
});
app.service('chatbot', function ($http, $rootScope) {
this.callAPI = function (user_entered_query) {
//I used detectintent REST API endpoint: find the project name from your account.
var endpoint = "https://dialogflow.googleapis.com/v2/projects/xxx/agent/sessions/123456789:detectIntent";
var data = JSON.stringify({queryParams:{}, query_input:{text:{text:user_entered_query,language_code:"en-US"}},outputAudioConfig:{},inputAudio:""});
var headers = {
//use the token from nodejs service
"Authorization": "Bearer " +$rootScope.token
};
return $http.post(_url, _data, {"headers": headers});
}
});
3.在客户端JavaScript中,使用从上述nodejs应用程序获得的访问令牌进行AJAX调用。以下是示例代码:
app.get("/your_sample_web_service_to_get_access_token", (req, res, next) => {
new Promise((resolve) => {
tokens.get({
//find this email value from the downloaded json
email: 'xxx@xxx.iam.gserviceaccount.com',
//find this key value from the downloaded json
key: '-----BEGIN PRIVATE KEY-----xxx',
//specify the scopes you wish to access: as mentioned in dialogflow documentation
scopes: ['https://www.googleapis.com/auth/cloud-platform']
},
(err, token) => {
//rest api response
res.json({
"access_token": token
});
resolve(token);
}
);
});
});
app.service('chatbot', function ($http, $rootScope) {
this.callAPI = function (user_entered_query) {
//I used detectintent REST API endpoint: find the project name from your account.
var endpoint = "https://dialogflow.googleapis.com/v2/projects/xxx/agent/sessions/123456789:detectIntent";
var data = JSON.stringify({queryParams:{}, query_input:{text:{text:user_entered_query,language_code:"en-US"}},outputAudioConfig:{},inputAudio:""});
var headers = {
//use the token from nodejs service
"Authorization": "Bearer " +$rootScope.token
};
return $http.post(_url, _data, {"headers": headers});
}
});
你找到解决办法了吗?我试图找到一种让js聊天弹出窗口直接与DialogFlow通信的方法,但它们的文档太乱了。。。如果没有中间件(比如NodeJS服务器),我不确定这是否可能。。。安全性高于易用性。。。但到了这个程度,这是荒谬的。。。我觉得找到另一个服务并放弃Dialogflow会更快…@Yoh我必须创建一个中间件作为Nodejs服务,它将处理身份验证并与DealogFlow API通信。对于v1,我不必这样做。是的,我也有同样的观点。太多了!你找到解决办法了吗?我试图找到一种让js聊天弹出窗口直接与DialogFlow通信的方法,但它们的文档太乱了。。。如果没有中间件(比如NodeJS服务器),我不确定这是否可能。。。安全性高于易用性。。。但到了这个程度,这是荒谬的。。。我觉得找到另一个服务并放弃Dialogflow会更快…@Yoh我必须创建一个中间件作为Nodejs服务,它将处理身份验证并与DealogFlow API通信。对于v1,我不必这样做。是的,我也有同样的观点。太多了!有人可以编辑代码格式吗。我正在使用Ctrl+k,但是,它不会格式化。有人可以编辑代码格式吗。我使用的是Ctrl+k,但它不会格式化。