【问题标题】:Testing Coinbase API with Postman : pagination gives me error用 Postman 测试 Coinbase API:分页给了我错误
【发布时间】:2022-01-08 08:29:48
【问题描述】:

我正在使用 Postman 测试 Coinbase API 端点,挑战在于何时需要分页

为了设置 Postman,我遵循了可用的指南 here,总结如下:

  1. 添加变量
    • coinbase-api-base
    • coinbase-api-key
    • coinbase-api-secret
    • coinbase-api-timestamp
    • coinbase-api-签名
  2. 添加了预请求脚本以生成请求签名
// 1. Import crypto-js library

var CryptoJS = require("crypto-js");

// 2. Create the JSON request object var req = { timestamp: Math.floor(Date.now() / 1000), // seconds since Unix epoch method:
pm.request.method, path: pm.request.url.getPath(), body: '', // empty
for GET requests message: undefined, secret:
pm.collectionVariables.get("coinbase-api-secret"), // read value from
collection variable hmac: undefined, signature: undefined, };

// 3. Create the message to be signed req.message = req.timestamp + req.method + req.path + req.body;

// 4. Create HMAC using message and API secret req.hmac = CryptoJS.HmacSHA256(req.message, req.secret);

// 5. Obtain signature by converting HMAC to hexadecimal String req.signature = req.hmac.toString(CryptoJS.enc.Hex);

// 6. Log the request console.info("request: ", req);

// 7. Set Postman request's authentication headers for Coinbase REST API call pm.collectionVariables.set("coinbase-api-timestamp",
req.timestamp); pm.collectionVariables.set("coinbase-api-signature",
req.signature);

对于一个简单的请求,一切都很好,例如:

获取 {{coinbase-api-base}}/v2/accounts

那么,如果我添加正文请求参数(如here 解释的那样):

限制=50

要更改默认分页,我得到一个身份验证错误....

“错误”:[ { "id": "authentication_error",
“消息”:“无效签名”
}

问题:

我该如何解决?

请求正文如何与请求签名配合使用...

非常感谢任何帮助建议

谢谢

【问题讨论】:

    标签: api authentication pagination coinbase-api


    【解决方案1】:

    编辑:下面说的是,我不确定基本帐户 API 是否支持分页,但我可能是错的,至少可以说 CB 文档不一致。不过,帐户历史记录(分类帐)和持有似乎确实如此。

    https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccounts

    Node.js API 中的获取帐户功能没有提供分类帐所提供的 args 参数(见下文):

    getAccounts(callback) {
      return this.get(['accounts'], callback);
    }
    

    支持分页的 api 的文档,请注意它为您提供了帐户文档中没有的查询参数部分:

    https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccountledger

    查看节点api,还是需要在body中添加查询字符串params才能签名:

    调用函数:

    return this.get(
      ['accounts', accountID, 'ledger'],
      { qs: args },
      callback
    );
    

    签名功能:

    let body = '';
    if (options.body) {
    body = JSON.stringify(options.body);
    } else if (options.qs && Object.keys(options.qs).length !== 0) {
        body = '?' + querystring.stringify(options.qs);
    }
    const what = timestamp + method.toUpperCase() + path + body;
    const key = Buffer.from(auth.secret, 'base64');
    const hmac = crypto.createHmac('sha256', key);
    const signature = hmac.update(what).digest('base64');
    return {
      key: auth.key,
      signature: signature,
      timestamp: timestamp,
      passphrase: auth.passphrase,
    };
    

    【讨论】:

      【解决方案2】:

      您不能将限制添加到请求的正文中,GET 请求从不包含任何正文。

      您应该将其添加为查询字符串参数,例如(这只是一个示例):

      GET {{coinbase-api-base}}/v2/accounts?limit=50   
      

      【讨论】:

      • 谢谢。我可能问错了;)我没有输入请求正文...我在url中添加了限制,突然之间似乎我用预请求脚本生成的签名无效...但是url中没有参数,它工作正常...
      猜你喜欢
      • 2022-09-30
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2021-01-12
      • 1970-01-01
      相关资源
      最近更新 更多