利用fetch返回API响应时遇到问题



一直在一个脚本执行在谷歌应用程序脚本拉一些数据从外部API和发布信息到谷歌表。我有一个脚本,客户端工作(从chrome控制台运行),能够利用承诺和返回HTTP响应正确执行更多的代码。

然而,在应用程序脚本我不知道如何从API返回一个本地JSON对象表示。在正常的JS中,我会返回响应的.json()方法,并且会很好。因为Apps Script本质上是执行一个.gs文件,他们有不同的类和方法,不是特定于JS。这个帮助文档https://developers.google.com/apps-script/guides/services/external提供了以下使用JSON

的示例
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data.title);

如果我试图单独利用getContextText()方法,我会得到TypeError: Cannot read property '0' of undefined错误。如果我把它和JSON.parse,比如return JSON.parse(response.getContentText());结合,就得到SyntaxError: Unexpected token M in JSON at position 0。我是不是错过了什么非常明显的东西?任何帮助将非常感激!此外,很高兴提供更多的细节从我的脚本以及。

编辑

下面是一个在客户端工作的脚本片段:

async function postData(url = '', data = {}) {

const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': "Basic" + ' ' + gongCreds,
'Accept': "*/*",
'Connection': "keep-alive",
'Content-Type': "application/json"
},
body: gongRequestBody,
});

return response.json(); 
}

这里是返回的承诺对象数据,我想在将来的执行中利用

[[PromiseResult]]: Object
records: {totalRecords: 1, currentPageSize: 1, currentPageNumber: 0}
requestId: "6w83fpcbo5ka2evast7"
usersDetailedActivities: Array(1)
0:
userDailyActivityStats: Array(1)
0:
callsAsHost: []
callsAttended: (6) ["432806286570218902", "1825323793748204011", "3193437184015561879", "4172384470445855263", "5128172192322468435", "5808052479141116583"]
callsCommentsGiven: []
callsCommentsReceived: []
callsGaveFeedback: []
callsMarkedAsFeedbackGiven: []
callsMarkedAsFeedbackReceived: []
callsReceivedFeedback: []
callsRequestedFeedback: []
callsScorecardsFilled: []
callsScorecardsReceived: []
callsSharedExternally: []
callsSharedInternally: []
fromDate: "2021-01-20T05:00:00Z"
othersCallsListenedTo: (2) ["3401282086024720458", "8098199458721511977"]

当你下面的Javascript转换为Google Apps Script时,

async function postData(url = '', data = {}) {

const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': "Basic" + ' ' + gongCreds,
'Accept': "*/*",
'Connection': "keep-alive",
'Content-Type': "application/json"
},
body: gongRequestBody,
});

return response.json(); 
}

变成如下:

const response = UrlFetchApp.fetch(url, {
method: 'POST',
headers: {
'Authorization': "Basic" + ' ' + gongCreds,
'Accept': "*/*",
'Connection': "keep-alive",
},
muteHttpExceptions: true,
contentType: "application/json",
payload: gongRequestBody,
});
console.log(response.getContentText())
// I think that if above request works and the returned value is the correct value you expected, you can use console.log(JSON.parse(response.getContentText()).title)

但是,有几个重要的点。

  1. 从你的脚本中,我看不到gongRequestBody的值。当使用'Content-Type': "application/json"时,需要为JSON对象使用JSON.stringify()。如果gongRequestBody是JSON对象,请使用JSON.stringify()将其转换为字符串。
  2. 从你的问题,我看不到你的脚本请求到URL。所以即使你的脚本有修改点,我也无法指出你脚本的修改点。
  3. 我要求在你的谷歌应用程序脚本中显示response.getContentText()的样本值。但不幸的是,我找不到它的样本值。所以当你使用console.log(JSON.parse(response.getContentText()).title)对上述Google Apps script的样例脚本出现错误时,你能提供console.log(response.getContentText())的样例值吗?通过这个,我想确认一下。

注意:

  • 在这个示例脚本中,它假设您的Javascript正常工作,并且变量gongCredsgongCredsgongRequestBody是请求URL的有效值。请小心这个。所以当上面的样例脚本没有像您期望的那样工作时,您能提供console.log(response.getContentText())的样例值吗?通过这个,我想确认你的情况。
  • 参考:

  • 获取(url, params)

最新更新