为什么API密钥身份验证在使用Crypto-JS的Coinbase Pro上不起作用?

时间:2019-07-08 08:30:26

标签: react-native cryptojs api-key coinbase-api gdax-api

我真的想通过React Native中的Crypto-JS库使API密钥身份验证与Coinbase Pro一起使用。

我已按照此处的说明进行操作: https://docs.pro.coinbase.com/#authentication

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

export const baseURL = 'https://api.pro.coinbase.com';
export const axiosTimeout = 10 * 1000; // 10 sec

export const addHeaders = (additional) => {
  const headers = {};
  return Object.assign(
    headers,
    {
      Accept: 'application/json',
      'User-Agent': 'rn-coinbasepro-app',
      'Content-Type': 'application/json'
    },
   additional
  );
};

componentDidMount() {
  const auth = {
    key: '',
    secret: '',
    passphrase: ''
  };

  const method = 'GET';
  const url = `${baseURL}/orders?status=all`;
  const timestamp = Date.now() / 1000;
  const requestPath = '/orders?status=all'
  const what = timestamp + method + requestPath;
  const key = Buffer.from(auth.secret, 'base64').toString('ascii');
  const hmac = CryptoJS.HmacSHA256(what, key);
  const signature = CryptoJS.enc.Base64.stringify(hmac);

  axios.interceptors.request.use((request) => {
    console.log('Starting Request', request);
    return request;
  });

  axios.interceptors.response.use((response) => {
    console.log('Response:', response);
    return response;
  });

  axios
    .get(url, {
      timeout: axiosTimeout,
      headers: addHeaders({
        method,
        timeout: axiosTimeout,
        'CB-ACCESS-KEY': auth.key,
        'CB-ACCESS-SIGN': signature,
        'CB-ACCESS-TIMESTAMP': timestamp,
        'CB-ACCESS-PASSPHRASE': auth.passphrase
      }),
      debug: true
    })
    .then((response) => {
      console.log(response);
      if (response.status === 200) {
        console.log(response.data);
      } else {
        console.log(response);
      }
    })
    .catch((error) => {
      console.log(error);
    });
}

它不断返回401(未经授权-无效的API密钥)

我已按照说明进行操作,并且我认为我已正确完成了所有操作。我只是看不到有什么问题,Coinbase Pro API很少返回任何解释原因。

0 个答案:

没有答案