sacOO7 / socketcluster-client-java

Native java and android client for socketcluster framework in node.js
http://socketcluster.io/
Apache License 2.0
94 stars 42 forks source link

How to authenticate connection with Coinigy? #17

Closed acz123454 closed 6 years ago

acz123454 commented 6 years ago

Hi,

As in Title, how should I put 'apiKey' and 'apiSecret' if I want authenticate connection using Yours lib to connect with Coinigy?

Best regards, Arek

sacOO7 commented 6 years ago

Hey I think I can point you to the example from where you can replicate for the same.


import logging
from socketclusterclient import Socketcluster
logging.basicConfig(format="%s(levelname)s:%(message)s", level=logging.DEBUG)

import json

api_credentials = json.loads('{}')
api_credentials["apiKey"]="xxx"
api_credentials["apiSecret"]="xxx"

def your_code_starts_here(socket):
    ###Code for subscription
    socket.subscribe('TRADE-OK--BTC--CNY')                 # Channel to be subscribed

    def channelmessage(key, data):                         # Messages will be received here
        print ("\n\n\nGot data "+json.dumps(data, sort_keys=True)+" from channel "+key)

    socket.onchannel('TRADE-OK--BTC--CNY', channelmessage) # This is used for watching messages over channel

    ###Code for emit

    def ack(eventname, error, data):
        print ("\n\n\nGot ack data " + json.dumps(data, sort_keys=True) + " and eventname is " + eventname)    

    socket.emitack("exchanges",None, ack)  

    socket.emitack("channels", "OK", ack)  

def onconnect(socket):
    logging.info("on connect got called")

def ondisconnect(socket):
    logging.info("on disconnect got called")

def onConnectError(socket, error):
    logging.info("On connect error got called")

def onSetAuthentication(socket, token):
    logging.info("Token received " + token)
    socket.setAuthtoken(token)

def onAuthentication(socket, isauthenticated):
    logging.info("Authenticated is " + str(isauthenticated))
    def ack(eventname, error, data):
        print ("token is "+ json.dumps(data, sort_keys=True))
        your_code_starts_here(socket);

    socket.emitack("auth", api_credentials, ack)

if __name__ == "__main__":
    socket = Socketcluster.socket("wss://sc-02.coinigy.com/socketcluster/")
    socket.setBasicListener(onconnect, ondisconnect, onConnectError)
    socket.setAuthenticationListener(onSetAuthentication, onAuthentication)
    socket.setreconnection(False)
    socket.connect()

This is a logic for python client, just replicate it in java. If there is still problem, let me know about it.

acz123454 commented 6 years ago

Hi,

Thanks for Yours answer! :)

I tried as below: String url = "wss://sc-02.coinigy.com/socketcluster/"; Socket socket = new Socket(url);

socket.setListener(new BasicListener() { public void onConnected(Socket socket, Map<String, List> headers) { System.out.println("Connected to endpoint"); }

public void onDisconnected(Socket socket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) { System.out.println("Disconnected from end-point"); }

public void onConnectError(Socket socket, WebSocketException exception) { System.out.println("Got connect error " + exception); }

public void onSetAuthToken(String token, Socket socket) { socket.setAuthToken(token); }

public void onAuthentication(Socket socket, Boolean status) { if (status) { System.out.println("socket is authenticated"); } else { System.out.println("Authentication is required (optional)");

     String apiCredentials =
           new Gson().toJson(new ApiCredentials("xxx",
                 "yyy"));

     socket.emit("auth", apiCredentials, new Ack() {
        @Override
        public void call(String name, Object error, Object data) {
           System.out.println("!!");
        }
     });

     Socket.Channel channel = socket.createChannel("zzz");
     channel.subscribe(new Ack() {
        public void call(String channelName, Object error, Object data) {
           if (error == null) {
              System.out.println("Subscribed to channel " + channelName + " successfully");
           }
        }
     });
  }

} }); and socket.connect(); Where: xxx - is my apiKey yyy - is my secretKey zzz - private channel ID

Firstly I got call in method “onAuthentication” error info: {"errorCode":403,"errorMsg":"Key/Secret Pair Failed”}

Secondly in incoming call in channel.subscribe: Your are connected but this socket has not been authenticated. Please emit auth event with credentials payload.

Any idea?

Thanks for all Your answers! :)

Best regards, Arek

On 10 Oct 2017, at 19:51, sachin shinde notifications@github.com wrote:

Hey I think I can point you to the example from where you can replicate for the same?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/sacOO7/socketcluster-client-java/issues/17#issuecomment-335554713, or mute the thread https://github.com/notifications/unsubscribe-auth/AfKjCT91nV_rSApI6cp8TzmZ18bbh4Daks5sq66fgaJpZM4P0FZS.

acz123454 commented 6 years ago

Also ApiCredentials is class as below: class ApiCredentials { String apiKey; String apiSecret;

public ApiCredentials() { }

public ApiCredentials(String apiKey, String apiSecret) { this.apiKey = apiKey; this.apiSecret = apiSecret; } }

On 11 Oct 2017, at 09:36, Arek Czarnowski arek.czarnowski@goodsoft.pl wrote:

Hi,

Thanks for Yours answer! :)

I tried as below: String url = "wss://sc-02.coinigy.com/socketcluster/ wss://sc-02.coinigy.com/socketcluster/"; Socket socket = new Socket(url);

socket.setListener(new BasicListener() { public void onConnected(Socket socket, Map<String, List> headers) { System.out.println("Connected to endpoint"); }

public void onDisconnected(Socket socket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) { System.out.println("Disconnected from end-point"); }

public void onConnectError(Socket socket, WebSocketException exception) { System.out.println("Got connect error " + exception); }

public void onSetAuthToken(String token, Socket socket) { socket.setAuthToken(token); }

public void onAuthentication(Socket socket, Boolean status) { if (status) { System.out.println("socket is authenticated"); } else { System.out.println("Authentication is required (optional)");

     String apiCredentials =
           new Gson().toJson(new ApiCredentials("xxx",
                 "yyy"));

     socket.emit("auth", apiCredentials, new Ack() {
        @Override
        public void call(String name, Object error, Object data) {
           System.out.println("!!");
        }
     });

     Socket.Channel channel = socket.createChannel("zzz");
     channel.subscribe(new Ack() {
        public void call(String channelName, Object error, Object data) {
           if (error == null) {
              System.out.println("Subscribed to channel " + channelName + " successfully");
           }
        }
     });
  }

} }); and socket.connect(); Where: xxx - is my apiKey yyy - is my secretKey zzz - private channel ID

Firstly I got call in method “onAuthentication” error info: {"errorCode":403,"errorMsg":"Key/Secret Pair Failed”}

Secondly in incoming call in channel.subscribe: Your are connected but this socket has not been authenticated. Please emit auth event with credentials payload.

Any idea?

Thanks for all Your answers! :)

Best regards, Arek

On 10 Oct 2017, at 19:51, sachin shinde <notifications@github.com mailto:notifications@github.com> wrote:

Hey I think I can point you to the example from where you can replicate for the same?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/sacOO7/socketcluster-client-java/issues/17#issuecomment-335554713, or mute the thread https://github.com/notifications/unsubscribe-auth/AfKjCT91nV_rSApI6cp8TzmZ18bbh4Daks5sq66fgaJpZM4P0FZS.

acz123454 commented 6 years ago

Did method as write below should be automatically called while connect?

public void onSetAuthToken(String token, Socket socket) { socket.setAuthToken(token); }

When I debugging, the method is not called.

sacOO7 commented 6 years ago

Hey instead of passing API key and value in the form of string, send it as a json object same as in python.

On Oct 11, 2017 3:58 PM, "acz1234" notifications@github.com wrote:

Did method as write below should be automatically called when connect?

public void onSetAuthToken(String token, Socket socket) { socket.setAuthToken(token); }

When I debugging, the method is not called.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sacOO7/socketcluster-client-java/issues/17#issuecomment-335766831, or mute the thread https://github.com/notifications/unsubscribe-auth/AP8vSTfM5DhqOzjpUn9hnuy-BM7WLzCBks5srJhGgaJpZM4P0FZS .

acz123454 commented 6 years ago

That is it! :D It works, it was problem in sending String , not Json object :)

Thanks!

sacOO7 commented 6 years ago

Ohk I'm closing the issue :). Please star and fork the repo. if you like it :+1: .

panekzogen commented 6 years ago

Hi, I'm using java sample written above and always get exception:

Got connect error com.neovisionaries.ws.client.WebSocketException: Failed to send an opening handshake request to the server: Received fatal alert: handshake_failure

Does it sample really work now?

panekzogen commented 6 years ago

I found solution for it. I used sample with java 7. But java 7 set old TLSv1 by default. And it caused this problem. If you set TLS to version v1.2 it will work.