【问题标题】:Google Apps Script: API authentication to Luno APIGoogle Apps 脚本:对 Luno API 的 API 身份验证
【发布时间】:2021-06-30 02:39:09
【问题描述】:

这里完全是菜鸟程序员(最初是一名老派机械工程师)。

在网上学习了一些 Javascript 课程后,我想我可以尝试在 Google Sheet 上构建一个简单的脚本。 我的项目计划是有一个脚本来通过他们的API 自动跟踪我在Luno.com(cryptoexchange)上的所有交易。 他们的API documentation is here。

我试图使用List Transactions 来填充我的电子表格中的所有历史交易,但我遇到了authenticate UrlFetchApp.fetch(url) endpoint 访问请求

编辑:您应该重新考虑公开分享您的身份验证信息。我删除了它。

下面是我的 Code.gs 脚本:

function importStatementLuno() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); //get active spreadsheet (bound to this script)
  var url = 'https://api.luno.com/api/1/accounts/xxx/transactions'; //api endpoint as a string

  var response = UrlFetchApp.fetch(url,{'muteHttpExceptions': false}); // get api endpoint
  var json = response.getContentText(); // get the response content as text
  var lunodata = JSON.parse(json); //parse text into json

Logger.log(lunodata);

我的 fetch 响应给了我这个错误:

异常:https://api.luno.com 的请求失败,返回代码 401。截断的服务器响应:未经授权 (使用 muteHttpExceptions 选项检查完整响应)

我一辈子都无法理解他们的文档!我不确定是否实际上有不同的表达式用于向他们的服务器提供身份验证密钥(他们明确提到他们在Get Started documentation 中更好地支持 Go) 我希望这里有人可以帮助我指出我哪里出错了。

【问题讨论】:

  • 您不应在此论坛上分享私人信息。如果您提供的身份验证信息允许其他人访问您的帐户,您可能应该考虑立即更改它。
  • 嗨@Zann,你能尝试下面答案中提到的基本身份验证吗?

标签: javascript google-apps-script


【解决方案1】:

基于Luno-Go documentation,它使用SetBasicAuth,根据Go documentation,SetBasicAuth只是HTTP Basic Authentication。如果可以解决您的问题,您可以尝试下面的代码吗?

代码:

var apiKeyID = 'apiKeyID';
var apiKeySecret = 'apiKeySecret';

var headers = {
  "Authorization": "Basic " + Utilities.base64Encode(apiKeyID + ":" + apiKeySecret)
};

var options = {
  "headers" : headers 
};

var response = UrlFetchApp.fetch(url, options); // get api endpoint

新错误:

  • 错误现在有所不同,因为我使用了无效的凭据。尝试在此处输入您的信息。

【讨论】:

  • 我使用有效的 api 密钥和秘密对此进行了测试,并且可以确认它有效。
猜你喜欢
  • 2015-01-31
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多