AgoraIO / API-Examples-Web

224 stars 199 forks source link

Invalid token, authorized failed #66

Closed pncsoares closed 2 years ago

pncsoares commented 2 years ago

Hi,

I am following the documentation https://docs.agora.io/en/voice-calling/develop/authentication-workflow

After applying all the steps I am facing the following error:

Agora-SDK [ERROR]: [client-729ce] join number: 1, Joining channel failed, rollback AgoraRTCException: AgoraRTCError CAN_NOT_GET_GATEWAY_SERVER: flag: 4096, message: AgoraRTCError CAN_NOT_GET_GATEWAY_SERVER: invalid token, authorized failed

This is what I have configured in agora.io console:

image

Could you please help? Thanks

plutoless commented 2 years ago

have you generated token and use it when joining channel? could you pls give me an example token you generated?

pncsoares commented 2 years ago

Yes I have. Example of a generated token by RtcTokenBuilder.buildTokenWithUid: 00630b9f4f0c35942538a8f567fda992004IADI/tNaitcwQh4k+YQ7nXBWu/heVQ0gnvwdJjvSv8RtjLO8GNtx9/GRIgAjCtQXZ5BCYwQAAQD3TEFjAgD3TEFjAwD3TEFjBAD3TEFj Here is the code that I am using to generate the token:

async generateToken(channel: string): Promise<string> {
    const appId = environment.agora.appId;
    const appCertificate = environment.agora.appCertificate;
    const channelName = channel;
    const role = RtcRole.PUBLISHER;

    const expirationTimeInSeconds = environment.agora.expirationTimeInSeconds;
    const currentTimestamp = Math.floor(Date.now() / 1000);
    const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds;

    // the uid that we use is the username but agora.io does not support string
    // so we will pass it as the current timestamp
    const uid = currentTimestamp;

    return RtcTokenBuilder.buildTokenWithUid(appId, appCertificate, channelName, uid, role, privilegeExpiredTs);
}

And here it is the code that I am using to join a voice call:

async joinVoiceCall(idUser: string, channel: string): Promise<boolean> {
    try {
      const agoraConfig = environment.agora;

      console.log('channel', channel);

      this.agoraEngine.on("user-published", async (user, mediaType) => {

        await this.agoraEngine.subscribe(user, mediaType);
        console.log("subscribe success");

        if (mediaType == "audio") {
          this.remoteUid = user.uid;

          this.remoteAudioTrack = user.audioTrack;

          this.remoteAudioTrack?.play();
          console.log("Remote user connected: " + user.uid);
        }

        this.agoraEngine.on("user-unpublished", user => {
          console.log(user.uid + "has left the channel");
        });
      });

      const uid: string = idUser;
      const appId: string = agoraConfig.appId;
      const channelName: string = channel;

      const token: string = await this.fetchToken(channelName);
      console.log('token', token);

      if (token === '' || token === undefined || token === null) {
        throw new Error("No token provided");
      }

      await this.agoraEngine.join(appId, channelName, token, uid);
      console.log("Joined channel: " + channelName);

      this.localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack();

      await this.agoraEngine.publish(this.localAudioTrack);
      console.log("Publish success!");

      return true;
    }
    catch (error) {
      console.error(error);
      return false;
    }
}
pncsoares commented 2 years ago

@plutoless do you have some feedback? Thanks

plutoless commented 2 years ago

i see the uid type you put here is string, while for our sdk it's actually required to be int. what kind of uid are you using here?

pncsoares commented 2 years ago

I am sending the username because I don't have a user ID as integer but I can pass a integer if the problem is that.

pncsoares commented 2 years ago

@plutoless I have tested with a number (12345) and now I got another error:

15:52:54:695 Agora-SDK [ERROR]: [client-013b0] join number: 1, Joining channel failed, rollback 
pncsoares commented 2 years ago

The data that I am sending to agora.io join method:

appId: cce2692fe7d443939970d5deafd41263 channelName: 1663866110273 token: 00630b9f4f0c35942538a8f567fda992004IACoAqW09I7Ro/ACeIxCuv3lGFngf4KETychQbJWLqhhnrO8GNsPr5LBIgAmQM3X7iZIYwQAAQB+40ZjAgB+40ZjAwB+40ZjBAB+40Zj uid: 12345

pncsoares commented 2 years ago

And here are the logs:

image

pncsoares commented 2 years ago

It is solved. The problem was with the uid. Thanks for helping.