Postman GET 请求 Binance API
Postman GET request to Binance API
我正在尝试向 Binance 的 API 发送 GET
请求,但我不知道该怎么做。
这是文档页面:https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#account-information-user_data
我有一个私人 apiKey
和 secretKey
。
我可以向 Binance 发出一般请求,但我无法使用我的私钥获取我的私人数据。
第一次尝试:
对于 Postman 中的 GET 请求,我使用这个字符串:
https://api.binance.com/api/v3/account?timestamp=1499827319559&signature=here_I_put_my_secret_key
我作为 header
通过了 Danny 建议 apiKey
。
但我得到:
{
"code": -1021,
"msg": "Timestamp for this request is outside of the recvWindow."
}
谢谢。
根据文档,这可能就是您想要的。
API-keys are passed into the Rest API via the X-MBX-APIKEY
header.
在您的请求中,将其添加为 header 键并将您的 API 键添加为值。
我在 Postman 中使用 javascript
解决了这个更正时间的问题。
另一个简单的解决方法是使用 ccxt
库:https://github.com/ccxt/ccxt
从这里获取 Binance API 的官方 Postman 集合:
https://github.com/binance/binance-api-postman
例如在 Postman 中导入所需的集合和环境
binance_spot_api_v1.postman_collection.json
和 binance_com_spot_api.postman_environment.json
将您的 API 密钥添加到 binance-api-key 环境变量并将您的密钥添加到 binance-api-秘密变量.
注意: 限制密钥在币安密钥管理中的作用。不要将此密钥用于生产,仅用于测试。为生产创建新密钥。
对于已签名的请求,请在预请求脚本中计算签名,然后设置 signature 环境变量。
示例预请求脚本:
function resolveQueryString() {
const query = JSON.parse(JSON.stringify(pm.request.url.query))
const keyPairs = []
for (param of query) {
if (param.key === 'signature') continue
if (param.disabled) continue
if (param.value === null) continue
const value = param.value.includes('{{') ? pm.environment.get(param.key) : param.value
keyPairs.push(`${param.key}=${value}`)
}
return keyPairs.join('&')
}
const signature = CryptoJS.HmacSHA256(
resolveQueryString(),
pm.environment.get('binance-api-secret')
).toString(CryptoJS.enc.Hex)
pm.environment.set('signature', signature)
你可以试试这个。这对我有用。只需替换您的 API_KEY 和 SECRET
您需要从 https://api.binance.com/api/v3/time 检索 serverTime 时间,并且需要使用该 serverTime 来签署请求。
GET : https://api.binance.com/api/v3/account?timestamp={{timestamp}}&signature={{signature}}
Header:
Content-Type:application/json
X-MBX-APIKEY:YOUR_API_KEY
Pre-request 脚本:
pm.sendRequest('https://api.binance.com/api/v3/time', function (err, res) {
console.log('Timestamp Response: '+res.json().serverTime);
pm.expect(err).to.not.be.ok;
var timestamp = res.json().serverTime;
postman.setEnvironmentVariable('timestamp',timestamp)
postman.setGlobalVariable('timestamp',timestamp)
let paramsObject = {};
const binance_api_secret = 'YOUR_API_SECRET';
const parameters = pm.request.url.query;
parameters.map((param) => {
if (param.key != 'signature' &&
param.key != 'timestamp' &&
!is_empty(param.value) &&
!is_disabled(param.disabled)) {
paramsObject[param.key] = param.value;
}
})
Object.assign(paramsObject, {'timestamp': timestamp});
if (binance_api_secret) {
const queryString = Object.keys(paramsObject).map((key) => {
return `${encodeURIComponent(key)}=${paramsObject[key]}`;
}).join('&');
console.log(queryString);
const signature = CryptoJS.HmacSHA256(queryString, binance_api_secret).toString();
pm.environment.set("signature", signature);
}
function is_disabled(str) {
return str == true;
}
function is_empty(str) {
if (typeof str == 'undefined' ||
!str ||
str.length === 0 ||
str === "" ||
!/[^\s]/.test(str) ||
/^\s*$/.test(str) ||
str.replace(/\s/g,"") === "")
{
return true;
}
else
{
return false;
}
}
}
);
我正在尝试向 Binance 的 API 发送 GET
请求,但我不知道该怎么做。
这是文档页面:https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#account-information-user_data
我有一个私人 apiKey
和 secretKey
。
我可以向 Binance 发出一般请求,但我无法使用我的私钥获取我的私人数据。
第一次尝试:
对于 Postman 中的 GET 请求,我使用这个字符串:
https://api.binance.com/api/v3/account?timestamp=1499827319559&signature=here_I_put_my_secret_key
我作为 header
通过了 Danny 建议 apiKey
。
但我得到:
{
"code": -1021,
"msg": "Timestamp for this request is outside of the recvWindow."
}
谢谢。
根据文档,这可能就是您想要的。
API-keys are passed into the Rest API via the
X-MBX-APIKEY
header.
在您的请求中,将其添加为 header 键并将您的 API 键添加为值。
我在 Postman 中使用 javascript
解决了这个更正时间的问题。
另一个简单的解决方法是使用 ccxt
库:https://github.com/ccxt/ccxt
从这里获取 Binance API 的官方 Postman 集合:
https://github.com/binance/binance-api-postman
例如在 Postman 中导入所需的集合和环境 binance_spot_api_v1.postman_collection.json 和 binance_com_spot_api.postman_environment.json
将您的 API 密钥添加到 binance-api-key 环境变量并将您的密钥添加到 binance-api-秘密变量.
注意: 限制密钥在币安密钥管理中的作用。不要将此密钥用于生产,仅用于测试。为生产创建新密钥。
对于已签名的请求,请在预请求脚本中计算签名,然后设置 signature 环境变量。
示例预请求脚本:
function resolveQueryString() {
const query = JSON.parse(JSON.stringify(pm.request.url.query))
const keyPairs = []
for (param of query) {
if (param.key === 'signature') continue
if (param.disabled) continue
if (param.value === null) continue
const value = param.value.includes('{{') ? pm.environment.get(param.key) : param.value
keyPairs.push(`${param.key}=${value}`)
}
return keyPairs.join('&')
}
const signature = CryptoJS.HmacSHA256(
resolveQueryString(),
pm.environment.get('binance-api-secret')
).toString(CryptoJS.enc.Hex)
pm.environment.set('signature', signature)
你可以试试这个。这对我有用。只需替换您的 API_KEY 和 SECRET
您需要从 https://api.binance.com/api/v3/time 检索 serverTime 时间,并且需要使用该 serverTime 来签署请求。
GET : https://api.binance.com/api/v3/account?timestamp={{timestamp}}&signature={{signature}}
Header:
Content-Type:application/json
X-MBX-APIKEY:YOUR_API_KEY
Pre-request 脚本:
pm.sendRequest('https://api.binance.com/api/v3/time', function (err, res) {
console.log('Timestamp Response: '+res.json().serverTime);
pm.expect(err).to.not.be.ok;
var timestamp = res.json().serverTime;
postman.setEnvironmentVariable('timestamp',timestamp)
postman.setGlobalVariable('timestamp',timestamp)
let paramsObject = {};
const binance_api_secret = 'YOUR_API_SECRET';
const parameters = pm.request.url.query;
parameters.map((param) => {
if (param.key != 'signature' &&
param.key != 'timestamp' &&
!is_empty(param.value) &&
!is_disabled(param.disabled)) {
paramsObject[param.key] = param.value;
}
})
Object.assign(paramsObject, {'timestamp': timestamp});
if (binance_api_secret) {
const queryString = Object.keys(paramsObject).map((key) => {
return `${encodeURIComponent(key)}=${paramsObject[key]}`;
}).join('&');
console.log(queryString);
const signature = CryptoJS.HmacSHA256(queryString, binance_api_secret).toString();
pm.environment.set("signature", signature);
}
function is_disabled(str) {
return str == true;
}
function is_empty(str) {
if (typeof str == 'undefined' ||
!str ||
str.length === 0 ||
str === "" ||
!/[^\s]/.test(str) ||
/^\s*$/.test(str) ||
str.replace(/\s/g,"") === "")
{
return true;
}
else
{
return false;
}
}
}
);