Resolving audio broadcasting error: client join failed DYNAMIC_KEY_EXPIRED (Agora.io)

Resolving audio broadcasting error: client join failed DYNAMIC_KEY_EXPIRED (Agora.io)

我是一名服务器端开发人员,具有基本的 JS 知识。我正在修补 Agora 的音频广播功能(专门针对网络)。作为参考,我一直在关注这个:https://docs.agora.io/en/Audio%20Broadcast/start_live_audio_web?platform=Web

我正在尝试以 host 的形式广播音频。我有一个 HTML 按钮,它触发一个 JS 函数,其中我:

我的理解是,完成上述内容将使我能够播放音频。当我尝试这个时,我最终得到一个 client join failed DYNAMIC_KEY_EXPIRED 错误。我找不到有关如何解决此问题的文档。你能帮我解决这个问题吗?一个说明性的例子会很好。

我的JS代码如下。请注意,我正在使用临时令牌在本地主机上测试此功能。

    // rtc object
    var rtc = {
      client: null,
      joined: false,
      published: false,
      localStream: null,
      remoteStreams: [],
      params: {}
    };

    // Options for joining a channel
    var option = {
      appID: "anAppID",// from 'Project Management' dashboard
      channel: "AudioLive",
      uid: null,//The user ID should be unique in a channel. If you set the user ID as null or 0, the Agora server assigns a user ID and returns it in the onSuccess callback.
      token: "aTempToken"// TEMP Token
    }
    
    function createBroadcast(role) {

        console.log("entered createBroadcast");
        
        // Create a client
        rtc.client = AgoraRTC.createClient({mode: "live", codec: "h264"});

        // Initialize the client
        rtc.client.init(option.appID, function () {
            console.log("init success");

            // Note: in a live broadcast, only the host can be heard and seen. You can also call setClientRole() to change the user role after joining a channel.
            rtc.client.setClientRole(role);
            console.log("role is set");

            // Call Client.join in the onSuccess callback of Client.init
            rtc.client.join(option.token ? option.token : null, option.channel, option.uid ? +option.uid : null, function (uid) {
                console.log("join channel: " + option.channel + " success, uid: " + uid);
                rtc.params.uid = uid;

                // Call AgoraRTC.createStream to create a stream in the onSuccess callback of Client.join
                rtc.localStream = AgoraRTC.createStream({
                    streamID: rtc.params.uid,
                    audio: true,
                    video: false,
                    screen: false,
                })

                // Call Stream.init to initialize the stream after 'creating' the stream above
                // Initialize the local stream
                rtc.localStream.init(function () {
                    
                    console.log("init local stream success");
                    
                    // play stream with html element id "local_stream"
                    rtc.localStream.play("local_stream");

                    // Call Client.publish in the onSuccess callback of Stream.init to publish the local stream
                    // Publish the local stream
                    rtc.client.publish(rtc.localStream, function (err) {
                      console.log("publish failed");
                      console.error(err);
                    })

                }, function (err) {
                  console.error("init local stream failed ", err);
                });


              }, function(err) {
                console.error("client join failed", err)
            })

          }, (err) => {
          console.error(err);
        });
    }
<div style="background:#f0f3f4;padding:20px">
    <button id="broadast" style="height:40px;width:200px" onclick="createBroadcast('host')">Start Live Broadcast</button>
</div>

我没有在上面的代码中添加 appIDtoken 的实际值。

注意:如果您需要,请询问更多信息。

您遇到的错误是由于在生成 APP ID 时为身份验证而生成的令牌已过期。要解决此问题,您将必须生成一个新令牌,如以下给定链接中所述:


Token-expired
renewToken


令牌(或临时令牌)在一段时间后过期。当SDK通过onTokenPrivilegeWillExpireonTokenPrivilegeDidExpire回调通知客户端token即将过期或已过期时,需要生成新的token并调用 renewToken 方法。

client.on("onTokenPrivilegeWillExpire", function(){
//After requesting a new token
client.renewToken(token);
});


client.on("onTokenPrivilegeDidExpire", function(){
//After requesting a new token
client.renewToken(token);
});

在您的 javascript 代码中包括上述函数以及其余的 eventListeners。
如果您的应用程序不需要安全性,您可以选择不使用令牌并生成没有证书的 App ID。
App ID without certificate

如果问题仍未解决,请回来寻求进一步支持。