0

I have searched through both stack overflow and google for the past few days with regards to this issue.

I am trying to connect to a 3rd party API therefore do not have access to or visibility of the websocket server.

I am able to connect to the OAuth endpoint and return a valid Access token however i am unable to pass this access token via socket.io-clients io.connect method without the access token being urlencoded.

Code that i am using to access the endpoint (with expired, but was valid, access token)

 serviceEndpoint = "/uapi/ws/" + sha1(appId + accessToken) + "/" + instanceId + "/";


    socket = io.connect(host, {
        transports: ['websocket'],
        path: serviceEndpoint,
        query: "access_token=1|5-zURhzg6X-vEWUwqBM~b9D0vqHJ-bBR|211|1455710158|7-ykMSvs07-w13mfhpf.CBNJEmmH.GY5"
    });

When getting the error back from the server (401) the url that is being requested has the following string for access_token:

access_token=1%7C5-zURhzg6X-vEWUwqBM~b9D0vqHJ-bBR%7C211%7C1455710158%7C7-ykMSvs07-w13mfhpf.CBNJEmmH.GY5

As you can see all of the pipe (|) characters have been encoded which is causing the token to be discarded by the server.

  • Is there a way to stop the query string from being urlencoded?
  • If not, is there a way to POST this parameter using socket.io-client?
Matt Nunn
  • 9
  • 7
  • I think your issue might be in the serviceEndpoint and not the query. I've read up on the API you are using and use socket.io in a number of projects. I've never had an issue with the url encoding and would suspect that the 3rd party API you are using is aware of this. Additionally, you have closely mirrored their example which leads me to believe your code is correct and more likely there is an issue with something not shown here. – Michael Hobbs Feb 17 '16 at 12:28
  • When you say an issue with something not shown here are you talking about code on my end or code on the API providers end? You are correct about it closely mirroring their example, to take out any external variations that might be introduced with my own code i have copied and pasted their example code with the only change being the sha1 of the appId and access token to mirror their API documentation – Matt Nunn Feb 17 '16 at 14:46
  • I suspect the issue has to do with code on your end specifically serviceEndpoint = "/uapi/ws/" + sha1(appId + accessToken) + "/" + instanceId + "/"; my gut is say something is going wrong with sha1(appId + accessToken) which is causing your 401. I feel like if you had a bad access token you would get a different error. – Michael Hobbs Feb 17 '16 at 16:52
  • @MichaelHobbs that is entirely a possibilty, however reading though the documentation again this is the description for device id: `deviceId String SHA1 (AppId + AccessToken), where AppId and AccessToken are also used for OAuth authentication.` and `instanceId String A random string that identifies the connection context. If you want to open multiple WebSockets for the same app, use different instanceId.` – Matt Nunn Feb 17 '16 at 17:26
  • What does your host string look like? – Michael Hobbs Feb 17 '16 at 17:55
  • @MichaelHobbs apologies for the delay the host string is a bog standard URL https://xxxx.xx.xxx.co.uk – Matt Nunn Feb 18 '16 at 09:29
  • np, on the delay. Is it http or https? Edit: I'm half guessing here as I see no reason why what you have done should not work. I have also looked through socket.io and socket.io-client source and see no easy way to stop the encoding. – Michael Hobbs Feb 18 '16 at 12:12
  • @MichaelHobbs it is https, the stackoverflow turned the comment into a link, could you suggest another way of connecting to the websocket other than socket.io that would allow me to pass the parameters in the url? – Matt Nunn Feb 18 '16 at 12:21
  • You will need to use the library ws. An example ws in use https://github.com/OgarProject/Ogar/blob/master/src/GameServer.js I've never worked with ws directly but have worked on the Ogar project some which uses it. Best I can offer, good luck! – Michael Hobbs Feb 18 '16 at 12:29

1 Answers1

0

So, after a lot more research and looking at other ways of doing it as well as discussions with the API providers one of the workarounds i gave discovered was setting the header on connect.

This Stack Overflow Answer Definitely helped with that. Note that the version of socket.io-client will need to be >= 1.4 for this to work

Community
  • 1
  • 1
Matt Nunn
  • 9
  • 7