Why do I still get error code 400 from this API request to Coinbase Pro?

天涯浪子 提交于 2019-12-11 17:11:37

问题


I've followed the instructions on the Coinbase Pro developers page for authentication with API Key and no matter what I try I always get error code 400 using CryptoJS and React / React Native.

https://docs.pro.coinbase.com/#api-key-permissions

import CryptoJS from 'crypto-js';
import axios from 'axios';
import { Buffer } from 'buffer';

const key = '<removed';
const secret = '<removed>';
const passphrase = '<removed>';
const method = 'GET';
const uri = 'https://api.pro.coinbase.com/accounts';
const requestPath = '/accounts';
const timestamp = Math.floor(Date.now() / 1000);
const message = `${timestamp}${method}${requestPath}`;
const secret64 = Buffer.from(secret, 'base64').toString('ascii');
const hmac = CryptoJS.HmacSHA512(message, secret64);
const signed = CryptoJS.enc.Base64.stringify(hmac).toString();

const headers = {
  'User-Agent': 'reactnative',
  'Content-Type': 'application/json',
  'CB-ACCESS-KEY': key,
  'CB-ACCESS-SIGN': signed,
  'CB-ACCESS-TIMESTAMP': timestamp,
  'CB-ACCESS-PASSPHRASE': passphrase,
  'CB-VERSION': '2019-05-22'
};

axios
  .get(uri, {
    method,
    headers
  })
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });



Other variations I have tried:

// const secret64 = Buffer.from(secret, 'base64').toString('ascii');
// const secret64 = Buffer.from(secret, 'base64').toString();
// const secret64 = Buffer.from(secret, 'base64');
// const signed = CryptoJS.HmacSHA512(message, secret64).toString();
// const signed64 = Buffer.from(signed).toString('base64');

// const hmac = CryptoJS.HmacSHA512(message, secret64).toString(CryptoJS.enc.Hex);
// const hmac = CryptoJS.HmacSHA512(message, secret64).toString(CryptoJS.enc.Hex);
// const signed = CryptoJS.enc.Base64.stringify(hmac);
// const signed = Buffer.from(hmac.toString()).toString('base64');
// const utf = CryptoJS.enc.Utf8.parse(hmac);
// const signed = CryptoJS.enc.Base64.stringify(utf).toString();
// const hash = CryptoJS.HmacSHA512(message, secret);
// const signed = CryptoJS.enc.Base64.stringify(hmac);

Error: Request failed with status code 400

What am I doing wrong?

来源:https://stackoverflow.com/questions/56392241/why-do-i-still-get-error-code-400-from-this-api-request-to-coinbase-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!