binary-live-api

I´m trying to test the binary-live-api, using the code example of the github page, but I´m only getting error messages.

My code:

var ws = require('ws');
var LiveApi = require('binary-live-api').LiveApi;

var api = new LiveApi({ websocket: ws });
api.authorize('My token'');
api.getPortfolio();
api.events.on('portfolio', function(data) {
ping();
});

Error message of node:

(node:2756) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AuthorizationRequired: [ServerError] Please log in.
{
"portfolio": 1,
"req_id": 2
}

What should I do?

Thank you.

Comments

  • @ealmachado said:
    I´m trying to test the binary-live-api, using the code example of the github page, but I´m only getting error messages.

    My code:

    var ws = require('ws');
    var LiveApi = require('binary-live-api').LiveApi;

    var api = new LiveApi({ websocket: ws });
    api.authorize('My token'');
    api.getPortfolio();
    api.events.on('portfolio', function(data) {
    ping();
    });

    Error message of node:

    (node:2756) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AuthorizationRequired: [ServerError] Please log in.
    {
    "portfolio": 1,
    "req_id": 2
    }

    What should I do?

    Thank you.

    Is token you entered something like ffbf4454h3blghfug ?

  • edited January 2017

    I've tried many tokens, including the oauth ones. Do you think it's only a token problem? I can't remember if it was like that ( ffbf4454h3blghfug). I'll try again and send you a picture.

  • I've tried de API token and the oauth token at the API Playground and the response was ok on both. But when I've tested them on node.js, using the binary-live-api, I've got the same error message.

  • edited January 2017

    This is an async call which means the code does not wait for authorization automatically. You need to wait for authorize response before you request for portfolio.
    One way is to listen for authorize event like you did for the portfolio response
    like this:

    var ws = require('ws');
    var LiveApi = require('binary-live-api').LiveApi;
    
    var api = new LiveApi({ websocket: ws });
    api.authorize('YourToken');
    api.events.on('authorize', function(data) {
      api.getPortfolio();
    });
    api.events.on('portfolio', function(data) {
      console.log(data);
    });
    

    Another way is to use new javascript capability called promises which is more readable but may not be compatible with older browsers.

    var ws = require('ws');
    var LiveApi = require('binary-live-api').LiveApi;
    
    var api = new LiveApi({ websocket: ws });
    api.authorize('YourToken').then(function resolve(resp){
      api.getPortfolio();
    });
    api.events.on('portfolio', function(data) {
      console.log(data);
    });
    

    BTW, using binary-live-api you don't need to ping manually, the library will take care of that for you.

Sign In or Register to comment.