How to make Authorization

I have made a small program with Javascript. I still can not make Buy Contract, balance and others.
This is from my computer to server :

{"authorize":"p30KLzwBIYpGa0"}

Server answered like this :

{"authorize":{"balance":"9870.77","country":"id","currency":"USD","email":"tonyismanto@yahoo.com","fullname":" ","is_virtual":1,"landing_company_fullname":"Binary Ltd","landing_company_name":"virtual","loginid":"VRTC1414067","scopes":["read","trade","payments","admin"]},"echo_req":{"authorize":"p30KLzwBIYpGa0"},"msg_type":"authorize"}

I tried to know my balance :

{"balance":1}

But the server said like this:
{"echo_req":{"balance":1},"error":{"code":"AuthorizationRequired","message":"Please log in."},"msg_type":"balance"}

I have tried it with API Playground. There is no problem.

Best Answer

  • Accepted Answer

    @Raunak, After receiving your help, now it is ok. Thank you very much for your help.

Answers

  • @tony_bot when you connect to websocket you first send {"authorize":"p30KLzwBIYpGa0"} call to authorize the connection and on same connection i.e when you get authorize response you can send balance call. Can you paste your code here so as to help you better?

  • edited March 7

    @Raunak , I have sent the file
    Thank You.

    var wsUri = "wss://ws.binaryws.com/websockets/v3?app_id=3143";
    var output;
    var flag_rdy = 0;
    var ticket;
    
    function init() {
     output = document.getElementById("output");
    }
    
    function f1() {
     document.getElementById("demo").innerHTML = testWebSocket_1();
    }
    
    function f2() {
     document.getElementById("demo").innerHTML = testWebSocket_2();
    }
    
    function f3() {
     document.getElementById("demo").innerHTML = testWebSocket_3();
    }
    
    function f4() {
     document.getElementById("demo").innerHTML = testWebSocket_4();
    }
    
    function testWebSocket_1() {
     websocket = new WebSocket(wsUri);
     websocket.onopen = function(evt) {
      onOpen_1(evt)
     };
     websocket.onclose = function(evt) {
      onClose_1(evt)
     };
     websocket.onmessage = function(evt) {
      onMessage_1(evt)
     };
     websocket.onerror = function(evt) {
      onError(evt)
     };
    };
    
    function testWebSocket_2() {
     websocket = new WebSocket(wsUri);
     websocket.onopen = function(evt) {
      onOpen_2(evt)
     };
     websocket.onclose = function(evt) {
      onClose_1(evt)
     };
     websocket.onmessage = function(evt) {
      onMessage_2(evt)
     };
     websocket.onerror = function(evt) {
      onError(evt)
     };
    }
    
    function testWebSocket_3() {
     websocket = new WebSocket(wsUri);
     websocket.onopen = function(evt) {
      onOpen_3(evt)
     };
     websocket.onclose = function(evt) {
      onClose_1(evt)
     };
     websocket.onmessage = function(evt) {
      onMessage_2(evt)
     };
     websocket.onerror = function(evt) {
      onError(evt)
     };
    }
    
    function testWebSocket_4() {
     websocket = new WebSocket(wsUri);
     websocket.onopen = function(evt) {
      onOpen_4(evt)
     };
     websocket.onclose = function(evt) {
      onClose_1(evt)
     };
     websocket.onmessage = function(evt) {
      onMessage_2(evt)
     };
     websocket.onerror = function(evt) {
      onError(evt)
     };
    }
    
    function onOpen_1(evt) {
     proposal();
    }
    
    function onOpen_2(evt) {
     authorize();
    }
    
    function onOpen_3(evt) {
     buy_contract(ticket);
    }
    
    function onOpen_4(evt) {
     balance();
    }
    
    function onMessage_1(evt) {
     obj = JSON.parse(evt.data);
     ticket = obj.proposal.id;
     writeToScreen('' + evt.data + '');
     writeToScreen('' + ticket + '');
     websocket.close();
    }
    
    function onMessage_2(evt) {
     obj = JSON.parse(evt.data);
     writeToScreen('' + evt.data + '');
     websocket.close();
    }
    
    function onClose_1(evt) {
     writeToScreen('disconnected');
    }
    
    function onError(evt) {
     writeToScreen('ERROR: ' + evt.data);
    }
    
    function doSend(message) {
     writeToScreen("SENT: " + message);
     websocket.send(message);
    }
    
    function writeToScreen(message) {
     var pre = document.createElement("p");
     pre.style.wordWrap = "break-word";
     pre.innerHTML = message;
     output.appendChild(pre);
    }
    
    function authorize() {
     doSend(JSON.stringify({
      'authorize': 'p30KLzwBIYpGa0z'
     }));
    }
    
    function proposal() {
     doSend(JSON.stringify({
      'proposal': 1,
      'amount': '1',
      'basis': 'payout',
      'contract_type': 'CALL',
      'currency': 'USD',
      'duration': '60',
      'duration_unit': 's',
      'symbol': 'frxUSDJPY'
     }));
    }
    
    function buy_contract(evt) {
     doSend(JSON.stringify({
      "buy": evt,
      "price": 100
     }));
    }
    
    function balance(evt) {
     doSend(JSON.stringify({
      "balance": 1
     }));
    }
    
    function ping() {
     doSend(JSON.stringify({
      "ping": 1
     }));
    }
    window.addEventListener("load", init, false);
    
  • @tony_bot you are opening a new connection for every call which is not a good practice, and when you send buy call over new connection, then that connection is not authorized hence you get authorization error

    Better to use callback as per your need

    var ws = new WebSocket('wss://ws.binaryws.com/websockets/v3?app_id=<app_id>');
    
    // send authorize when connection is open, this way calls made on this ws are authorized
    ws.onopen = function (e) {
        console.log("i am open send request", e);
        ws.send(JSON.stringify({authorize: "<token>"}));
    }
    
    var result;
    // this is called whenever websocket receive response from server
    ws.onmessage = function (msg) {
      result = JSON.parse(msg.data);
      if (result["msg_type"] === "authorize") {
          console.log(result);
          // send proposal when you get authorize
          ws.send(JSON.stringify({ <proposal_json>}));
      // check for ticks history
      } else if (result["msg_type"] === "proposal"){
          ws.send(JSON.stringify({ <buy_json>}));
      }
    };
    

    if you want to attach events then keep ws object in your scope and onClick of buy do this ws.send(JSON.stringify({buy: <proposal_id>, price: 5})

  • what do you mean with price:5 in ws.send(JSON.stringify({buy: <proposal_id>, price: 5}) raunak

    help?

Sign In or Register to comment.