Ruby中的Agora RTC令牌生成创建无效令牌

dgenwo3n  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(154)

我正在我的Ruby应用程序中设置Agora's authentication workflow,但是当我在Agora Web Demo上测试时,我生成的所有令牌都给予AgoraRTCError CAN_NOT_GET_GATEWAY_SERVER: flag: 4096
来自Web控制台的完整错误是:

Agora-SDK [ERROR]: [client-48eb2] 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

我使用AgoraDynamicKey2::RtcTokenBuilderAgoraIO/Tools Github Repo我使用的具体文件是RTC Token Builder
我实现的代码是:

require 'agora_dynamic_key2'
# app/models/chat/participant.rb
# Chat::Participant Model
# This is the model which connects users to chats.
class Chat::Participant < ApplicationRecord

  def agora_token
    app_id = Rails.application.credentials.dig(:agora, :app_id)
    app_certificate = Rails.application.credentials.dig(:agora, :app_certificate)
    channel_name = chat.id
    account = user.id
    token_expiration_in_seconds = 3600
    privilege_expiration_in_seconds = 3600

    AgoraDynamicKey2::RtcTokenBuilder.build_token_with_user_account(
      app_id, app_certificate, channel_name, account,
      AgoraDynamicKey2::RtcTokenBuilder::ROLE_PUBLISHER, token_expiration_in_seconds, privilege_expiration_in_seconds
    )
  end

end

chat.iduser.id都是UUID字符串。
RTC令牌代码来自Agora库:

def self.build_token_with_user_account(app_id, app_certificate, channel_name, account, role, token_expire, privilege_expire = 0)
      access_token = AgoraDynamicKey2::AccessToken.new(app_id, app_certificate, token_expire)
      service_rtc = AgoraDynamicKey2::ServiceRtc.new(channel_name, account)

      service_rtc.add_privilege(AgoraDynamicKey2::ServiceRtc::PRIVILEGE_JOIN_CHANNEL, privilege_expire)
      if role == ROLE_PUBLISHER
        service_rtc.add_privilege(AgoraDynamicKey2::ServiceRtc::PRIVILEGE_PUBLISH_AUDIO_STREAM, privilege_expire)
        service_rtc.add_privilege(AgoraDynamicKey2::ServiceRtc::PRIVILEGE_PUBLISH_VIDEO_STREAM, privilege_expire)
        service_rtc.add_privilege(AgoraDynamicKey2::ServiceRtc::PRIVILEGE_PUBLISH_DATA_STREAM, privilege_expire)
      end
      access_token.add_service(service_rtc)
      access_token.build
    end

我有做错什么吗?有没有人知道一种更好的方法来检查令牌的有效性,并提供更好的错误消息?

vzgqcmou

vzgqcmou1#

看起来,至少对于AgoraDynamicKey2,帐户或uid值必须是整数,理想情况下是32位无符号整数。(128位在测试中工作,但未被Flutter SDK接受)
不支持将字符串值作为account传递,尽管文档和API注解中暗示了这一点。
为了解决这个问题,我们从用户的UUID生成一个32位int,

# on the User model

  def uid_32
    id[0, 8].hex
  end

然后在聊天参与者模型上,

def agora_token
    app_id = Rails.application.credentials.dig(:agora, :app_id)
    app_certificate = Rails.application.credentials.dig(:agora, :app_certificate)
    channel_name = chat_id
    uid = user.uid_32
    role = AgoraDynamicKey2::RtcTokenBuilder::ROLE_PUBLISHER
    token_expiration_in_seconds = 3600
    privilege_expiration_in_seconds = 3600

    AgoraDynamicKey2::RtcTokenBuilder.build_token_with_user_account(
      app_id, app_certificate, channel_name, uid, role, token_expiration_in_seconds, privilege_expiration_in_seconds
    )
  end

这背后的原因看起来是,从AgoraDynamicKey移动到AgoraDynamicKey2时,删除了对生成带有字符串“account”的RTC令牌的支持,如这里的代码注解所示:动态密钥DynmaicKey2
他们GitHub上的examples that Agora Provide确实隐式地显示了这一变化,但不清楚这是一个关键的变化,错误也没有帮助。

相关问题