【问题标题】:Receiving a 401 'truncated server' error when trying to add a Stripe API Key to UrlFetchApp in Google Apps Scripts尝试在 Google Apps 脚本中将 Stripe API 密钥添加到 UrlFetchApp 时收到 401“截断服务器”错误
【发布时间】:2019-04-12 20:28:25
【问题描述】:

我正在尝试通过 Stripe API 在 Google 表格中 retrieve an invoice,并且我正在使用 Google Apps 脚本来运行我的代码。这是我的代码:

function callNumbers() 
  {
    var url = "https://api.stripe.com/v1/invoices/[unique_id]";
    var apiKey = "[api-key]";

    var response = UrlFetchApp.fetch(url, {"headers":{"[api-key]":apiKey}}); 
    var content = response.getContentText(); 
    Logger.log(response); 
    Logger.log(content);
 }

Stripe 发票 URL 在上面的 /invoices/ 之后的 URL 中有一个唯一 ID,代表我要检索的特定测试发票。

我使用的是test Stripe API key,它在唯一的 API 密钥代码之前以此开头:sk_test_

Stripe 页面上的引用如下所示:

curl https://api.stripe.com/v1/invoices/[unique_id] \
  -u sk_test_[api-key]:

我收到的错误信息如下所示:

Request failed for https://api.stripe.com/v1/invoices/[unique_id] returned code 401. Truncated server response: { "error": { "message": "You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g... (use muteHttpExceptions option to examine full response) (line 13, file "Code")

我不确定我的代码有什么问题。我猜这与"headers" 部分有关,但我对 API 的了解还不够,无法确定。我正在使用这个测试项目来尝试了解有关 API 的更多信息。

【问题讨论】:

  • 我怀疑你的问题是标题部分在做什么。您可以使用基本身份验证,也可以使用承载身份验证。你在做〜也许是基本的?这不是很清楚。我可能只是将您的标题更改为Authorization : Bearer sk_test_xxx。我怀疑看起来像这样:{"headers":{"Authorization" : "Bearer " + apiKey}}

标签: javascript google-apps-script google-sheets stripe-payments


【解决方案1】:

试试这样的:

function getInvoiceObj(invoiceURL) {
  var content,options,response ,secret,url;

  secret = 'sk_test_ABC123';//sk_live_ABC123'

  url = "https://api.stripe.com/v1/invoices/" + invoiceURL;

  options = {
    "method" : "GET",
    "headers": {
      "Authorization": "Bearer " + secret
    },
    "muteHttpExceptions":true
  };

  response = UrlFetchApp.fetch(url, options);

  content = response.getContentText(); 
  Logger.log(response); 
  Logger.log(content);
}

【讨论】:

  • 嗨艾伦,解决了我的问题!非常感谢!对于将来阅读这篇文章以供参考的任何人,它看起来对我有用,而无需使用第五行中的 apiKey url = "https://api.stripe.com/v1/invoices/" + apiKey; 而不是使用 + apiKey 您可以只使用看起来像 @ 的唯一发票 url 987654324@
猜你喜欢
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 2011-11-26
相关资源
最近更新 更多