Javascript 接收401';截断的服务器';尝试在Google Apps脚本中将条带API密钥添加到UrlFetchApp时出错

Javascript 接收401';截断的服务器';尝试在Google Apps脚本中将条带API密钥添加到UrlFetchApp时出错,javascript,google-apps-script,google-sheets,stripe-payments,Javascript,Google Apps Script,Google Sheets,Stripe Payments,我正试图通过StripeAPI在Google工作表中创建应用程序,我正在使用Google应用程序脚本来运行我的代码。以下是我的代码: function callNumbers() { var url = "https://api.stripe.com/v1/invoices/[unique_id]"; var apiKey = "[api-key]"; var response = UrlFetchApp.fetch(url, {"headers":{"[api-

我正试图通过StripeAPI在Google工作表中创建应用程序,我正在使用Google应用程序脚本来运行我的代码。以下是我的代码:

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,表示我要检索的特定测试发票

我使用的是一个,它在唯一API密钥代码之前以这个开头:
sk\u test\uu

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

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")
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);
}
我收到的错误消息如下所示:

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

我不确定我的代码出了什么问题。我猜这是关于
“headers”
部分的,但我对API的了解还不够确切。我正在使用此测试项目,试图了解更多有关API的信息。

尝试以下内容:

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

我怀疑你的问题在于标题部分正在做什么。您可以使用基本身份验证,也可以使用承载身份验证。你在做~也许是基本的?不太清楚。我可能会将您的标题更改为
授权:Bearer sk_test\u xxx
。我怀疑会是这样:
{“headers”:{“Authorization”:“Bearer”+apiKey}}
你好,艾伦,这解决了我的问题!非常感谢!对于将来阅读此文章以供参考的任何人来说,它似乎对我有效,而无需使用第五行中的apiKey
url=”https://api.stripe.com/v1/invoices/“+apiKey
而不是使用
+apiKey
您可以只使用唯一的发票url,它看起来像
https://api.stripe.com/v1/invoices/in_xxxxxxxxxxxxxxxxxxxxxx