0

I am trying to hit a private WebSocket server through javascript client code. This WebSocket is hosted on GCP as a private cloud-run service. Can you please help me to hit this WebSocket by passing the "Authorization" token as part of the header?

I am currently calling the WebSocket using this approach

const socket = new WebSocket('wss://web-adaptor-service-lz7ymxgtha-ue.a.run.app');

I just need to pass the Authorization bearer token along wiht this url. Please help me with this.

NOTE : I am able to hit the same websocket by passing the required header using postman. I am attaching that snippet herewith for your reference. Please check it out. I just want to achieve the same using javascript client code

Thanks in advance!!!

I tried to connect the websocket server from javascript client code by passing the headers in the below mentioned ways but all ultimately failed. I am everytime getting 403 error during doing this connectivity.

const websocketUrl = 'wss://web-adaptor-service-lz7ymxgtha-ue.a.run.app';
const gcp_auth_token = 'eyJhbGciOiJSU**************************';

1.1 socket = new WebSocket(websocketUrl + '?Authorization=Bearer ' + gcp_auth_token);
1.2 socket = new WebSocket(websocketUrl + '?Origin=' + gcp_auth_token);

1.3 socket = new webSocket(websocketUrl, null, {
        headers: {
            ['Authorization']: 'Bearer ' + gcp_auth_token
        }
    });
1.4 socket = new WebSocket(websocketUrl + '?access_token=' + gcp_auth_token);
1.5 socket = new WebSocket("wss://<gcp_auth_token>@<websocketUrl>");
1.6 socket = new WebSocket(websocketUrl , ["access_token", `${gcp_auth_token}']);
1.7 socket = new WebSocket(websocketUrl , [
        "base64url.bearer.authorization.k8s.io.<base64encodedtoken>", 
        "base64.binary.k8s.io"
    ]);  //this format is give in this link  => https://github.com/kubernetes/kubernetes/pull/47740 which is also failing
Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
Ankita
  • 1
  • 2
  • You can send in query => new WebSocket("ws://localhost:8080/api/SocketDock?authorization=eyJhbGciOiJIUzI1...") – Rustam Ansari Jan 13 '23 at 13:34
  • Hi @rustam-ansari. Thanks for replying on my question. I actually tried the way u mentioned here but it still didnt work. I have tried in all the possible ways[mentioned in my post] but nothing is actually working. Where as , the same token is working while connecting to the websocket through postman. – Ankita Jan 16 '23 at 11:01
  • Please go throgth this link -- https://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api ---- You can also try this one --- const ws = new WebSocket(WS_URL, { headers: { Auth: "token" } }) – Rustam Ansari Jan 16 '23 at 11:35

0 Answers0