【问题标题】:Initiating Salesforce API in Google App Script [duplicate]在 Google 应用程序脚本中启动 Salesforce API [重复]
【发布时间】:2021-05-24 18:41:14
【问题描述】:

更新

这个全面的答案解决了我的问题:

https://stackoverflow.com/a/29112224/1045794


我在看https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_code.htm

获得 Salesforce 版本后,我尝试获取资源列表。

这是我访问 3rd 方 API 的正常方式,但会导致 401 错误,就好像我未经授权一样。我相信我使用的是正确的令牌,但也许这实际上是一个错误的请求。

var url = 'https://company.lightning.force.com/services/data/v51.0/';

var myBody = '';
var headers = {"Authorization" : 'Bearer ' + 'my_token'};

var params = {
   'method': 'GET',
   //'muteHttpExceptions': true,
   'headers': headers,
   'contentType': 'application/json',
   //'payload': myBody
};

var res = UrlFetchApp.fetch(url, params);

var myObj = JSON.parse(res.getContentText());

Logger.log(myObj)

编辑 - 我知道 URL 是正确的,因为测试 https://company.lightning.force.com/services/data/ 确实会返回版本号

编辑 2 - 将 URL 更改为 https://company.my.salesforce.com/services/data/v50.0/ 会导致相同的错误

【问题讨论】:

  • 您是否尝试过使用 company.salesforce.com/services/data/v51.0/ URL 代替 company.lightning.force.com?
  • @वरुण DNS 错误
  • 您在刷新令牌吗?因为令牌在一段时间内有效。您需要获取刷新令牌。 salesforce.stackexchange.com/questions/73512/…
  • 谢谢@वरुण,我想我的问题是,这看起来像是访问 API 的有效方法吗?如果是,那么它一定是令牌。我也会调查的。
  • 这看起来像是访问 API 的有效方法。如果您没有刷新可能导致问题的令牌。

标签: javascript google-apps-script salesforce bearer-token


【解决方案1】:

我相信您应该将 Headers 而不是普通对象传递给 fetch 参数:

var params = {
   'method': 'GET',
   //'muteHttpExceptions': true,
   'headers': new Headers(headers),
   'contentType': 'application/json',
   //'payload': myBody
};

【讨论】:

  • 这会返回 ReferenceError: Headers is not defined
  • 看起来你的浏览器不支持这个,试试 window.Headers 或者一些 polyfill
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多