Techioz Blog

Ruby での Agora RTC トークン生成により無効なトークンが作成される

概要

Ruby アプリで Agora の認証ワークフローを設定しようとしていますが、Agora Web デモでテストすると、生成したすべてのトークンで 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

AgoraIO/Tools Github リポジトリの AgoraDynamicKey2::RtcTokenBuilder を使用しています 私が使用している特定のファイルは RTC トークン ビルダーです

私が実装したコードは次のとおりです。

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.id と user.id はどちらも UUID 文字列です。

RTC トークン コードは Agora lib からのものです。

    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

ここで私が間違っていることはありますか? より適切なエラーメッセージでトークンの有効性をチェックするより良い方法を知っている人はいますか?

解決策

少なくとも AgoraDynamicKey2 では、アカウントまたは uid の値は整数、理想的には 32 ビットの符号なし整数でなければならないようです。 (テストでは 28 ビットは動作しましたが、Flutter SDK では受け入れられませんでした)

ドキュメントや 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 トークンの生成のサポートが削除されたためであると考えられます。 ダイナミックキー ダイナミックキー2

Agora が GitHub で提供している例では、この変更が暗黙的に示されていますが、これが重要な変更であるかどうかは明らかではなく、エラーは役に立ちませんでした。