我已经创建了GAS应用程序,以通过Hubspot实例提供更好的管道报告。到目前为止,该应用程序可以正常运行,并且我已经成功创建了一个显示在Google表格中的销售渠道。我试图添加一项功能,该功能需要POST方法来查询Hubspot的CRM V3。我可以在Postman中使用它,但是不能在GAS中复制它。
我收到的错误是“找不到身份验证凭据。”标头会打印到日志中,因此我认为它们是正确生成的。我的猜测是在请求期间,我的访问令牌和有效负载没有正确传递给API。在此问题上的任何帮助将不胜感激。
function getConversions() {
// Prepare authentication to Hubspot
var service = getService();
var headers = {headers: {'Authorization': 'Bearer ' + service.getAccessToken()}};
Logger.log(headers);
var raw = JSON.stringify({"filterGroups":[{"filters":[{"propertyName":"hs_analytics_last_visit_timestamp","operator":"GT","value":"1561514165666"}]}],"limit":100,"after":0});
var options = {
'method' : 'post',
headers: headers,
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
body : raw,
redirect: 'follow',
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch('https://api.hubapi.com/crm/v3/objects/contacts/search?', options);
var result = JSON.parse(response.getContentText());
Logger.log(result);
};
此修改如何?
当我检查the official document of Search of HubSpot API时,我发现了卷曲样品。当此示例转换为Google Apps脚本时,我注意到您的脚本中有几个修改点。
UrlFetchApp.fetch
不具有body
和redirect
的属性。关于followRedirects
,正式文件说如下。
如果为false,则提取不会自动遵循HTTP重定向;它返回原始的HTTP响应。默认值为true。
https://api.hubapi.com/crm/v3/objects/contacts/search?
。如果您不使用API密钥,则如何修改为https://api.hubapi.com/crm/v3/objects/contacts/search
?当上述修改反映到您的脚本时,它变成如下。
请进行如下修改。
从:var options = {
'method' : 'post',
headers: headers,
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
body : raw,
redirect: 'follow',
"muteHttpExceptions": true
};
至:var options = {
method : 'post',
headers: headers,
contentType: 'application/json',
payload : raw,
muteHttpExceptions: true
};
Authentication credentials not found.
的错误。在此修改中,假设您的访问令牌service.getAccessToken()
可用于此请求。当我看到正式文档时,也可以使用API密钥。如果无法使用访问令牌,那么如何使用API密钥?就像下面。https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=YOUR_HUBSPOT_API_KEY