Swagger客户端API密钥身份验证

时间:2017-12-28 20:16:04

标签: node.js swagger

我必须使用我的Api密钥对Bitmex API进行身份验证,但是我收到了此错误

无法连接:TypeError:无法读取未定义的属性“add”

'use strict';
var SwaggerClient = require("swagger-client");
var _ = require('lodash');
var BitMEXAPIKeyAuthorization = require('./lib/BitMEXAPIKeyAuthorization');

require('dotenv').config();

new SwaggerClient({
  // Switch this to `www.bitmex.com` when you're ready to try it out for real.
  // Don't forget the `www`!
  url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
  usePromise: true
})
.then(function(client) {
  //console.log(client);
  // Comment out if you're not requesting any user data.
  client.clientAuthorizations.add("apiKey", new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET));

  // Print client capabilities
  //

})
.catch(function(e) {
  console.error("Unable to connect:", e);
})

Nodejs连接器:https://github.com/BitMEX/api-connectors

1 个答案:

答案 0 :(得分:2)

您使用的是最新版本的客户端,该版本不以这种方式进行授权:https://github.com/swagger-api/swagger-js/blob/903569948d5a5c718d7b87d6832a672de4e76afc/docs/MIGRATION_2_X.md#authorizations

new SwaggerClient({
    // Switch this to `www.bitmex.com` when you're ready to try it out for real.
    // Don't forget the `www`!
    url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
    usePromise: true,
    authorizations: {
        apiKey: new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET)
    }
})
.then(client => {
    // Do whatever with client
})
.catch(function(e) {
console.error("Unable to connect:", e);
})