Search code examples
javascriptphptokenagora.io

Agora.io DYNAMIC_USE_STATIC_KEY with generated RTC Token (php)


I have a problem: I get DYNAMIC_USE_STATIC_KEY in console trying to establish a connection.

2 Tokens which are generated by an Agora Console and rtcTokenBuilder are different, the second is longer, and using it occurs this error. With console Token is everything ok.

Token examples: Console: 0068bedd2631cf24edea47479b64ad219ceIAAYXDdhxjKQyDWQEoqK0qEiAYo+dLSyd/YNAaflVD4PJO/ZQc0AAAAAEACMMKzfQDg1YAEAAQA/ODVg

rtcTokenBuilder: 0068bedd2631cf24edea47479b64ad219ceIAADpmWghbJIToZQMC9YrQjBhtFgpdZS4tIlOkXTjk+LKe/ZQc0h39v0IgCa0wUAjQY2YAQAAQA5tjRgAwA5tjRgAgA5tjRgBAA5tjRg

rtcTokenBuilder Code snippet:

$appID = "8*************************";
$appCertificate = "+++++++++++++++++++++++++++7";
$channelName = "Test Chamber";
$uid = 0;
$role = RtcTokenBuilder::RoleAttendee;
$expireTimeInSeconds = 300; // 5 min for test purposes
$currentTimestamp = (new DateTime("now", new DateTimeZone('UTC')))->getTimestamp();
$privilegeExpiredTs = $currentTimestamp + $expireTimeInSeconds;
$token = RtcTokenBuilder::buildTokenWithUid($appID, $appCertificate, $channelName, $uid, $role, $privilegeExpiredTs);
echo $token;

js file:

client.join(token, room, 0, (uid) => { // 0 -> uid
    let localStream = AgoraRTC.createStream({
        audio: true,
        video: true
    });
    localStream.init(() => 
      .....

client.on("onTokenPrivilegeWillExpire", function(){
    $.ajax({
        url: tokenGenPath, // path to rtcTokenBuilder php generator  
        method: "POST",
        success: function(data){
           newToken = data;
        },
        error: function(){
        }
    });
    client.renewToken(newToken);
});

It says here, that I do not provide a Token prior connection: Agora Docs

P.S. I've noticed also, if I generate a Token in Agora console for the start and in case of expiration of this Token a new valid Token is generated from rtcTokenBuilder and connection/conversation continues. enter image description here


Solution

  • So, the problem was at 2 places. First, I changed AJAX Call

    From

    client.on("onTokenPrivilegeWillExpire", function(){
      $.ajax({
        url: tokenGenPath, // path to rtcTokenBuilder php generator  
        method: "POST",
        success: function(data){
           newToken = data;
        },
        error: function(){
        }
     });
      client.renewToken(newToken);
    });
    

    To:

    client.on("onTokenPrivilegeWillExpire", function(){
       $.post(tokenGenPath, function (token){
          client.renewToken(token);
        });
    });
    

    The second, I moved first Token generation out of the main function with client initialisation:

    $.post(tokenGenPath, function (data){
       token = data;    
    });
    
    function runApp (room, client, token){
        client.join(token, room, 0, (uid) => { // 0 -> uid
           let localStream = AgoraRTC.createStream({
           audio: true,
           video: true
        });
        localStream.init(() => 
            .....
    }
    

    enter image description here