【问题标题】:Postman GET request to Binance API邮递员 GET 请求 Binance API
【发布时间】:2020-07-23 03:10:28
【问题描述】:

我正在尝试向 Binance 的 API 发送一个GET 请求,但我不知道该怎么做。 这是文档页面:https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#account-information-user_data

我有一个私人apiKeysecretKey。 我可以向 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."
    }

谢谢。

【问题讨论】:

  • 嗯,文档中提到了大约 28 次 recvWindow,所以这是一个很好的起点。 "建议使用5000以下的小recvWindow!最大不能超过60000!"
  • 我认为阅读文档会对您有所帮助 - github.com/binance-exchange/binance-official-api-docs/blob/…
  • 感谢您的回复,我已经尝试设置 recvWindow=3000 或 50000 但它仍然会产生错误..
  • 我也尝试更改时间戳值,但没有任何改变..

标签: post get request postman binance


【解决方案1】:

根据文档,这可能是您所追求的。

https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#endpoint-security-type

API 密钥通过 X-MBX-APIKEY 标头传递到 Rest API。

在您的请求中,将其添加为标头密钥,并将您的 API 密钥添加为值。

【讨论】:

  • 感谢您的回复。我现在正在用我的进步更新我的问题。我是使用 API 的初学者...
【解决方案2】:

我在 Postman 中使用 javascript 解决了这个更正时间问题。 另一个简单的解决方法是使用 ccxt 库:https://github.com/ccxt/ccxt

【讨论】:

    【解决方案3】:

    从此处获取 Binance API 的官方 Postman 集合:

    https://github.com/binance/binance-api-postman

    例如在 Postman 中导入所需的集合和环境 binance_spot_api_v1.postman_collection.jsonbinance_com_spot_api.postman_environment.json

    将您的 API 密钥添加到 binance-api-key 环境变量,并将您的密钥添加到 binance-api-secret 变量。

    注意:限制密钥在币安密钥管理中的作用。不要将此密钥用于生产,仅用于测试。为生产创建新密钥。

    对于已签名的请求,请在预请求脚本中计算签名,然后设置 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)
    

    【讨论】:

      【解决方案4】:

      你可以试试这个。这对我有用。只需替换您的 API_KEY 和 SECRET

      您需要从 https://api.binance.com/api/v3/time 检索 serverTime 时间,并且需要使用该 serverTime 来签署请求。

      GET : https://api.binance.com/api/v3/account?timestamp={{timestamp}}&signature={{signature}}
      

      标题:

      Content-Type:application/json
      X-MBX-APIKEY:YOUR_API_KEY
      

      预请求脚本:

      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;
                  }
              }
          }
      ); 
      

      【讨论】:

        猜你喜欢
        • 2021-10-26
        • 2018-05-18
        • 2019-01-07
        • 2021-01-17
        • 1970-01-01
        • 2019-08-25
        • 2021-09-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多