我已经按照Coinbase Pro开发人员页面上的说明使用API密钥进行身份验证,无论我怎么尝试,我总是使用CryptoJS和React / React Native收到错误代码400。
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);
错误:请求失败,状态码为400
我在做什么错了?