AgoraIO / Basic-Video-Broadcasting

Sample app to join/leave a channel, set the role as a host/audience, mute/unmute, switch between front/rear cameras, and set the video parameters.
MIT License
269 stars 287 forks source link

Not able to use agora renewToken #124

Open peterujah opened 3 years ago

peterujah commented 3 years ago

I created live streaming on agora android. Before opening the live activity I always check from server if token is valid and if no generate new token using the agora php SDK like below.

<?php 
   Include_once(/vendor/Agora/RtcTokenBuilder.php');
   $appID = "MY_APP_ID";
    $appCertificate = "MY_APP_CERT";
    $channelName = $app->CreateRandomChannelName();
    $uid = 0;
    $role = RtcTokenBuilder::RolePublisher;
    $expireTimeInSeconds = 3600;
    $currentTimestamp = (new DateTime("now", new DateTimeZone('UTC')))->getTimestamp();
    $privilegeExpiredTs = $currentTimestamp + $expireTimeInSeconds;

    $token = RtcTokenBuilder::buildTokenWithUid($appID, $appCertificate, $channelName, $uid, $role, $privilegeExpiredTs);
    $response =  array(
        "status" => 200,
        "token" => $token,
        "channel" => $channelName,
    );
?>

And call rtcEngine().renewToken("tokeFromServer..."); then also set the channel name which I used in generating token 'config().setChannelName(liveName);, yet it doesn't work except when i add the token instring_config.xml. I also tried adding the token which I generated from agora server to a static string then pass it tortcEngine().renewToken("tokeFromServerStatic");` but it seems not working.

Please how do I renewToken before starting live?

plutoless commented 3 years ago

@peterujah just generate a new token every time, renewToken is used if you have already joined channel, while the live broadcasting is so long that it exceeds the expire time you set for the original token, there will be a tokenWillExpire callback from sdk and then you call renewToken to renew it while in channel. This is usually not needed, just generate a new token every time.