簡體   English   中英

嘗試在Google Apps腳本中向UrlFetchApp添加Stripe API密鑰時收到401“截斷服務器”錯誤

[英]Receiving a 401 'truncated server' error when trying to add a Stripe API Key to UrlFetchApp in Google Apps Scripts

我正在嘗試通過Stripe API在Google表格中檢索發票 ,並且正在使用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);
 }

條紋發票URL在/ invoices /之后的上方url中具有唯一ID,代表我要檢索的特定測試發票。

我正在使用測試Stripe API密鑰 ,該代碼以唯一的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的更多信息。

嘗試這樣的事情:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM