0

我正在尝试在 Google Apps 脚本中使用 StackExchange API V2.2。

当我尝试发送 POST 请求以获取访问令牌时,问题出现在显式 OAuth 2.0 流程的最后一步。我收到 404 错误,但手动发出相同的请求(使用邮递员扩展)一切正常。

为了简化问题,如果我发送这个没有有效负载的 POST 请求,我会收到相同的 404

var response = UrlFetchApp.fetch("https://stackexchange.com/oauth/access_token", {
  method: 'post',
  muteHttpExceptions: true
});
Logger.log(response);

在邮递员中,我收到此 400:

{
   "error": {
       "type": "invalid_request",
       "message": "client_id not provided"
   }
}

我想这将是 UrlFetchApp 的问题,但有人知道如何解决吗?谢谢!

4

2 回答 2

0

该问题与 Origin 标头有关。

您不能直接从标头中删除,但可以通过代理执行调用:)

于 2015-01-05T16:56:19.633 回答
0

您需要通过向调用添加“选项”对象来为帖子提供数据。例如:

var options = { "method" : "post", "payload" : payload };
UrlFetchApp.fetch("https://stackexchange.com/oauth/access_token", options);

顺便说一句,您是否尝试过使用 UrlFetch 获得的 OAuth:https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#addOAuthService(String) - 这可能是更好的方法。

于 2014-12-03T11:52:27.497 回答