尝试在Google Apps脚本中向UrlFetchApp添加Stripe API密钥时收到401“截断服务器”错误

时间:2019-04-12 20:28:26

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

我正在尝试通过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_

开头

“条带化”页面上的引用如下所示:

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的更多信息。

1 个答案:

答案 0 :(得分: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);
}