【问题标题】:Non Pro CoinBase API Invalid Signature非 Pro CoinBase API 签名无效
【发布时间】:2021-10-25 21:04:57
【问题描述】:

我一直在尝试通过他们的 API 从非专业 CB 那里获取一些数据。

我已经能够从 coinbase pro 获取数据,使用下面的代码,添加密码,但没有 CB 骰子...

我不断收到无效签名:(

知道我可能缺少什么吗?

const signedMessages = async (timestamp, meth, requestPath) => {
  const secret = process.env.cb_all_read_secret;
  const method = meth.toUpperCase();
  const body = '';

  const message = timestamp + method + requestPath + body;
  const key = Buffer.from(secret, 'base64');
  const hmac = crypto.createHmac('sha256', key);
  const cb_access_sign = hmac.update(message).digest('base64');
  return cb_access_sign;
};

const listAccounts = async () => {
  let timestamp = Math.floor(Date.now() / 1000);
  const signed = await signedMessages(timestamp, 'GET', '/v2/accounts');
const url = 'https://api.coinbase.com/v2/accounts';

  const options = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'CB-ACCESS-KEY': process.env.cb_all_read_key,
      'CB-ACCESS-SIGN': signed,
      'CB-ACCESS-TIMESTAMP': timestamp,
    },
  };
  console.log(options);

  try {
    const data = await fetch(url, options);
    const resultat = await data.json();
    console.log(resultat);
  } catch (error) {
    console.log('error: ', error.message);
  }
};

listAccounts();

【问题讨论】:

    标签: api coinbase-api


    【解决方案1】:

    我要检查的第一件事是您的计算机时钟是最新的,即不落后 API 服务器 > 30 秒。

    【讨论】:

    • 已经检查过了,我们有相同的时间。起初我有一个时间戳错误,地板让它消失了..
    【解决方案2】:

    coinbase 文档充其量是令人沮丧的。 在一个地方,我看到您正在使用的方法被记录在案。

    var what = timestamp + method + requestPath + body;
    
    // decode the base64 secret
    var key = Buffer(secret, 'base64');
    
    // create a sha256 hmac with the secret
    var hmac = crypto.createHmac('sha256', key);
    
    // sign the require message with the hmac
    // and finally base64 encode the result
    return hmac.update(what).digest('base64');
    

    当我再次找到它时,我发现了以下内容:documentation 签名略有不同:

    var signature = crypto.createHmac("sha256", apiSecret).update(message).digest("hex");
    

    请注意缺少base64 缓冲区,摘要为Hex

    我修改了您的代码,并且能够获得具有查看权限的钱包。我可能会使用提供的链接中的代码创建一个签名函数,包括您需要包含的任何选项的正文。

    const signedMessages = async (timestamp, method, path) => {
        const apiSecret = '...';
        const bodyStr = '';
        let message = timestamp + method.toUpperCase() + '/v2/' + path + bodyStr;
        return crypto.createHmac('sha256', apiSecret).update(message).digest('hex');
    };
    
    const listAccounts = async () => {
        let timestamp = Math.floor(Date.now() / 1000);
        const signed = await signedMessages(timestamp, 'GET', 'accounts');
        const url = 'https://api.coinbase.com/v2/accounts/';
    
        const options = {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'CB-ACCESS-KEY': '...',
                'CB-ACCESS-SIGN': signed,
                'CB-ACCESS-TIMESTAMP': timestamp,
            },
        };
        console.log(options);
        try {
            const data = await fetch(url, options);
            const resultat = await data.json();
            console.log(resultat);
        } catch (error) {
            console.log('error: ', error.message);
        }
    };
    
    listAccounts();
    

    但你猜怎么着......

    "deprecated" node api 以同样的方式执行此操作(在我找到文档之前,我就在此处找到了此签名方法),并且它最后一次更新是在 4 年前。去图吧。

    【讨论】:

      猜你喜欢
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 2019-01-12
      • 2015-04-21
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      相关资源
      最近更新 更多