3

Having problem getting security token generated from my server to work properly. I modified the basicLive.js of the demo in AgoraWebSDK-NG to work with my server. I'm using the Golang api provided by Agora to generate the token. Some information:

AppID: ---
App Certificate: ---
channel name: liveshow10
uid: 51495 (I assume Agora does not care about this as long as it is uint32)
token expiration seconds: 3600
generated token: 006cb08a5a2ddc049f79f29d9b52e5909b0IABKKxbI64sFWnkyEICQrL+0DeTsrN9ckKMkDj/oir36aV+tq536WyGcIgCHHmUFHNv6XgQAAQCsl/leAgCsl/leAwCsl/leBACsl/le

But I'm getting bunch of error in the console. I think it boils down to an authorization error the token generated somehow is invalid. Below is the browser console and thanks for any help:

enter image description here

Hermes
  • 2,828
  • 2
  • 14
  • 34
Joe Lin
  • 450
  • 7
  • 20
  • if you hardcode the token, does it give you an error? Are you implementing the "token-will-expire" and "did-expire" callbacks? – Hermes Jun 30 '20 at 11:57
  • Are you using String or int UID? – Hermes Jul 02 '20 at 16:30
  • I'm using uint32 UID. I tried once again generating token with RtcTokenBuilderSample.py in the python example. And I ran the basicLive demo supplying the generated token. Same error still. Agora server still complains about invalid token. – Joe Lin Jul 03 '20 at 04:40
  • Tried again with String. Same error:"AgoraRTCError CAN_NOT_GET_GATEWAY_SERVER: invalid token, authorized failed". Btw, hardcoding the token didn't help either, same error. – Joe Lin Jul 03 '20 at 04:49
  • I just tested that AppId without the Token and it connects without issue (https://digitallysavvy.github.io/group-video-chat/) and (https://webdemo.agora.io/agora-web-showcase/examples/Agora-Web-Tutorial-1to1-Web). This means the tokens are not setup correctly on your account, so no token will work. can you send me an email hermes@agora.io – Hermes Jul 03 '20 at 15:28

1 Answers1

10

For anyone following along, the issue was related to UID. In the above code the token is generate specifically for UID 51495 but in the Agora Demo, the JoinChannel method passes null as the UID value which tells the SDK to generate its own UID for that user.

When attempting to join a channel with a different UID than the one used to generate the token, you will receive an error.

If you want to generate a token that can be used by any user, you need to set the UID to 0 in the token generation, and then you can use any UID join the channel with this token.

Update: The UID 0 detail can be found in the Token Server API reference within the UID parameter description docs.agora.io/en/video/token_server#api-reference

Hermes
  • 2,828
  • 2
  • 14
  • 34
  • 3
    Wow... I cant believe i made such a silly mistake.... The UID was the first cause of my problem and then secondly, I had not enabled the app certificates. After i did the above mentioned, it worked very well. Thank you so much for this answer! – blessing dickson Dec 29 '20 at 18:17
  • 1
    I felt UID was very optional like in the token generated from Agora console. Your answer helped me with my Agora flutter + PHP backend – Eaweb Mar 12 '21 at 19:44
  • @Eaweb how were you able to generate token from the backend, anytime I generate token on the backend and try to use it on the client (almost immediately), I get token expired – Temi 'Topsy' Bello May 13 '21 at 22:45
  • How are you calculating expiration time? Take a look at my guide on token generation using nodejs https://www.agora.io/en/blog/how-to-build-a-token-server-for-agora-applications-using-nodejs/ – Hermes May 14 '21 at 02:04
  • Hi @Temi'Topsy'Bello make sure you are joining the channel with the same UID you used for generating the token on the backend. It will be worth checking the expiry time too. – Eaweb May 14 '21 at 11:29
  • @Hermes, ' If you want to generate a token that can be used by any user, you need to set the UID to 0 in the token generation, and then you can use any UID join the channel with this token.'- can you tell where it is said in the Agora doc ? – Istiaque Ahmed Jan 28 '22 at 11:29
  • It’s in the Token Server API reference within the UID parameter description https://docs.agora.io/en/video/token_server#api-reference – Hermes Jan 30 '22 at 20:42