Google apps script 使用Google Apps脚本签署对Coinbase Pro API的调用
尝试使用谷歌应用程序脚本将我的第一个API调用发送到Coinbase Pro时,我都快疯了。在node.js中是非常简单的(),但是在Google脚本中执行同样的操作只是一次又一次地返回“无效签名” 这是我正在使用的代码: 函数GetMyAccounts(){ 变量globalvars\u CB={ “apikey”:“f7d20a**********18c”, “秘密”:“******pIIitRbWCv9N/mMWaR****mGQMuI+m/vSbU1zuh5U6WFiFw==”, “密码短语”:“ceacdsewfcsa”, 'uri':'https://api.pro.coinbase.com' } var requestPath='/accounts'; var timestamp=Math.floor(Date.now()/1000); 变量选项={ '方法':'获取', “muteHttpExceptions”:true, “标题”:{ “内容类型”:“应用程序/json”, “CB-ACCESS-KEY”:globalvars_CB.apikey, “CB-ACCESS-SIGN”:SignAPICall(globalvars_CB.secret,时间戳'GET',requestPath',), “CB-ACCESS-TIMESTAMP”:时间戳, “CB-ACCESS-PASSPHRASE”:globalvars_CB.PASSPHRASE, } } var responseJson=UrlFetchApp.fetch(globalvars\u CB.uri+requestPath,选项); Logger.log(responseJson); } 函数SignAPICall(秘密、时间戳、方法、请求路径、正文){ var what=(时间戳+方法+请求路径+正文); var decodedsecret=Utilities.base64Decode(secret.toString(); var hmac=Utilities.computeHmacSha256Signature(what,decodedsecret); hmac=实用程序.base64Encode(hmac); 返回(hmac);Google apps script 使用Google Apps脚本签署对Coinbase Pro API的调用,google-apps-script,base64,sha256,coinbase-api,Google Apps Script,Base64,Sha256,Coinbase Api,尝试使用谷歌应用程序脚本将我的第一个API调用发送到Coinbase Pro时,我都快疯了。在node.js中是非常简单的(),但是在Google脚本中执行同样的操作只是一次又一次地返回“无效签名” 这是我正在使用的代码: 函数GetMyAccounts(){ 变量globalvars\u CB={ “apikey”:“f7d20a**********18c”, “秘密”:“******pIIitRbWCv9N/mMWaR****mGQMuI+m/vSbU1zuh5U6WFiFw==”, “密
}在脚本中,它假定您的请求头(CB-ACCESS-SIGN和endpoint的值除外)是正确的。请小心这个 修改点:
- 对于
,数组将转换为字符串。我认为这可能是你的问题的原因实用程序.base64Decode(secret).toString()
SignAPICall
function SignAPICall(secret, timestamp, method, requestPath, body) {
var what = (timestamp + method + requestPath + body);
var decodedsecret = Utilities.base64Decode(secret); // Modified
var res = Utilities.computeHmacSha256Signature(Utilities.newBlob(what).getBytes(), decodedsecret); // Modified
hmac = Utilities.base64Encode(res);
return hmac;
}
- 在这种情况下,
的computeHmacSha256Signature(value,key)
和value
是字节数组key
- 当我通过比较的示例脚本来检查上面修改的脚本时,我可以确认可以获得相同的结果
- 不幸的是,我无法使用上述修改的脚本测试对API的请求,同时我可以确认从上述修改的脚本中检索到了来自正式文档中示例脚本的相同签名。因此,请在您的环境中测试请求。当您使用上面修改的脚本请求API时,如果出现错误,请再次检查请求头、端点和密码
- 我终于找到了解决办法。我提交给“Utilities.computeHmacSha256Signature”的数据类型有问题。在代码中,您可以发现该函数运行良好
函数SignAndCallAPI(方法、请求路径、正文){
var timestamp=Math.floor(Date.now()/1000).toString();
var what=Utilities.base64Decode(Utilities.base64Encode(timestamp+method+requestPath+body));
var decodedsecret=实用程序.base64Decode(globalvars\u CB.secret);
var hmac=Utilities.base64Encode(Utilities.computeHmacSha256Signature(what,decodedsecret));
变量选项={
“方法”:方法,
“muteHttpExceptions”:true,
“标题”:{
“内容类型”:“应用程序/json”,
“CB-ACCESS-KEY”:globalvars_CB.apikey,
“CB-ACCESS-SIGN”:hmac,
“CB-ACCESS-TIMESTAMP”:时间戳,
“CB-ACCESS-PASSPHRASE”:globalvars_CB.PASSPHRASE,
}
}
var responseJson=UrlFetchApp.fetch(globalvars\u CB.uri+requestPath,选项);
Logger.log(responseJson);
返回(responseJson);
}
我建议修改脚本作为答案。你能确认一下吗?不幸的是,我无法使用上面修改的脚本测试对API的请求。那么,您可以在您的环境中测试请求吗?如果这不是直接的解决办法,我道歉。我的回答是否向你展示了你想要的结果?你能告诉我这件事吗?这对我的学习也很有用。如果这样做有效,其他与你有相同问题的人也可以将你的问题作为可以解决的问题。如果你对我的回答还有疑问,我道歉。那时候,我可以问一下你目前的情况吗?我想学习来解决你的问题。嘿,谢谢你的回复。我以为我已经回答了。。。您的更改无效,原因是函数“Utilities.computeHmacSha256Signature”需要两个字符串或两个字节[],而不是它们的组合。我将发布正确的代码,使这个工作良好。谢谢你的回复。我的回答建议使用computeHmacSha256Signature(byte,byte)
,因为您使用的是var decodedsecret=Utilities.base64Decode(secret)
。所以我使用了Utilities.newBlob(what).getBytes()
。在您的补充回答中,我认为Utilities.base64Decode(Utilities.base64Encode(timestamp+method+requestPath+body))
与Utilities.newBlob(what).getBytes()相同。所以我无法理解我的答案和你的答案之间的区别。我为此道歉。