为什么我的 iOS 应用无法使用 Node.js Agora 令牌服务器创建的令牌通过 AgoraRtcEngineKit 进行身份验证?

Why can't my iOS app authenticate with AgoraRtcEngineKit using tokens created by a Node.js Agora Token Server?

问题总结

我的目标是使用一个用 SwiftUI 编写的 iOS 应用程序来连接 AgoraRtcEngineKit。我想创建一个纯音频应用程序,允许主持人广播音频并允许听众收听。

Agora 要求使用代币

我根据此处找到的 Agora 教程使用 Node.js 创建了一个 Agora 令牌服务器:https://www.agora.io/en/blog/how-to-build-a-token-server-for-agora-applications-using-nodejs/

这是来自我的 Agora-Node-TokenServer 的 index.js。此代码基于此处找到的 Agora 教程:https://github.com/digitallysavvy/Agora-Node-TokenServer/blob/master/index.js

const express = require('express')
const path = require('path')
const {RtcTokenBuilder, RtcRole} = require('agora-access-token');

const PORT = process.env.PORT || 5000

if (!(process.env.APP_ID && process.env.APP_CERTIFICATE)) {
  throw new Error('You must define an APP_ID and APP_CERTIFICATE');
}
const APP_ID = process.env.APP_ID;
const APP_CERTIFICATE = process.env.APP_CERTIFICATE;

const app = express();

const nocache = (req, resp, next) => {
  resp.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
  resp.header('Expires', '-1');
  resp.header('Pragma', 'no-cache');
  next();
  };


const generateAccessToken = (req, resp) => { 
  resp.header('Access-Control-Allow-Origin', '*');

  const channelName = req.query.channelName;if (!channelName) {
    return resp.status(500).json({ 'error': 'channel is required' });
  }

  
  // get uid 
  let uid = req.query.uid;
  if(!uid || uid == '') {
    uid = 0;
  }

  // get rtc role
  let rtcrole = RtcRole.SUBSCRIBER;
  if (req.query.rtcrole == 'publisher') {
    rtcrole = RtcRole.PUBLISHER;
  }


  // get the expire time
  let expireTime = req.query.expireTime;
  if (!expireTime || expireTime == '') {
    expireTime = 3600;
  } else {
    expireTime = parseInt(expireTime, 10);
  }
  // calculate privilege expire time
  const currentTime = Math.floor(Date.now() / 1000);
  const privilegeExpireTime = currentTime + expireTime;

  const rtctoken = RtcTokenBuilder.buildTokenWithUid(APP_ID, APP_CERTIFICATE, channelName, uid, rtcrole, privilegeExpireTime);
 
  return resp.json({ 'rtctoken': rtctoken });

};

app.get('/access_token', nocache, generateAccessToken);

app.listen(PORT, () => {
  console.log(`Listening on port: ${PORT}`);
});

如我所料 我期望的是能够使用 Agora 成功验证我的 iOS 应用程序。

实际结果 我无法通过 Agora 进行身份验证。

  1. 我的 Node.js 令牌服务器成功地向我的 iOS 应用程序发送了一个令牌。
  2. 但是当我使用 rtckit.joinChannel(byToken:...) 将所述令牌提交给 Agora 时,没有任何反应。从未到达 joinChannel 的完成块。
  3. 作为实验,我使用 rtckit.joinChannel(byToken:...) 从 Agora 的控制台向 Agora 提交了 'temporary token' 并且这个令牌被成功接受并且到达了完成块。

我试过的

下面是我的 iOS 应用程序中的相关代码,供参考。我将此代码基于此处的 Agora 教程:https://github.com/maxxfrazer/Agora-iOS-Swift-Example/blob/main/Agora-iOS-Example/AgoraToken.swift and here: https://www.agora.io/en/blog/creating-live-audio-chat-rooms-with-swiftui/

AgoraToken

import Foundation

class AgoraToken {

    /// Error types to expect from fetchToken on failing ot retrieve valid token.
    enum TokenError: Error {
        case noData
        case invalidData
        case invalidURL
    }

    /// Requests the token from our backend token service
    /// - Parameter urlBase: base URL specifying where the token server is located
    /// - Parameter channelName: Name of the channel we're requesting for
    /// - Parameter uid: User ID of the user trying to join (0 for any user)
    /// - Parameter callback: Callback method for returning either the string token or error
    static func fetchToken(
        urlBase: String, channelName: String, uid: UInt,
        callback: @escaping (Result<String, Error>) -> Void
    ) {
        guard let fullURL = URL(string: "\(urlBase)/?channelName=\(channelName)/&uid=\(uid)/") else {
            callback(.failure(TokenError.invalidURL))
            return
        }
        print("fullURL yields \(fullURL)")
        var request = URLRequest(
            url: fullURL,
            timeoutInterval: 10
        )
        request.httpMethod = "GET"

        let task = URLSession.shared.dataTask(with: request) { data, response, err in
            print("Within URLSession.shared.dataTask response is \(String(describing: response)) and err is \(String(describing: err))")
            if let dataExists = data {
                print(String(bytes: dataExists, encoding: String.Encoding.utf8) ?? "URLSession no data exists")
            }

            guard let data = data else {
                if let err = err {
                    callback(.failure(err))
                } else {
                    callback(.failure(TokenError.noData))
                }
                return
            }
            let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
            if let responseDict = responseJSON as? [String: Any], let rtctoken = responseDict["rtctoken"] as? String {
                print("rtc token is \(rtctoken)")
                callback(.success(rtctoken))
            } else {
                callback(.failure(TokenError.invalidData))
            }
        }

        task.resume()
    }
    
}

播客文件

# Uncomment the next line to define a global platform for your project
platform :ios, '14.8.1'

target 'AgoraPractice' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for AgoraPractice
  pod 'AgoraRtm_iOS' 
  pod 'AgoraAudio_iOS' 

  target 'AgoraPracticeTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'AgoraPracticeUITests' do
    # Pods for testing
  end

end

ContentView

import SwiftUI
import CoreData
import AgoraRtcKit



struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext

    @State var joinedChannel: Bool = false
    @ObservedObject var agoraObservable = AgoraObservable()
    
       var body: some View {
           Form {
               Section(header: Text("Channel Information")) {
                   TextField(
                       "Channel Name", text: $agoraObservable.channelName
                   ).disabled(joinedChannel)
                   TextField(
                       "Username", text: $agoraObservable.username
                   ).disabled(joinedChannel)
               }
               Button(action: {
                   joinedChannel.toggle()
                   if !joinedChannel {
                       self.agoraObservable.leaveChannel()
                   } else {
                       self.agoraObservable.checkIfChannelTokenExists()
                   }
               }, label: {
                   Text("\(joinedChannel ? "Leave" : "Join") Channel")
                       .accentColor(joinedChannel ? .red : .blue)
               })
               if joinedChannel {

                   Button(action: {
                       agoraObservable.rtckit.setClientRole(.audience)
                   }, label: {
                       Text("Become audience member")
                   })

                   Button(action: {
                       agoraObservable.rtckit.setClientRole(.broadcaster)
                   }, label: {
                       Text("Become broadcasting member")
                   })

               }
           }
       }
   }

   struct UserData: Codable {
       var rtcId: UInt
       var username: String
       func toJSONString() throws -> String? {
           let jsonData = try JSONEncoder().encode(self)
           return String(data: jsonData, encoding: .utf8)
       }
   }

   class AgoraObservable: NSObject, ObservableObject {
       
       @Published var remoteUserIDs: Set<UInt> = []
       @Published var channelName: String = ""
       @Published var username: String = ""

       
       // Temp Token and Channel Name
       var tempToken:String = "XXXXXXX"
       var tempChannelName:String = "XXXXX"
       
       var rtcId: UInt = 0
       //var rtcId: UInt = 1264211369
       
       let tokenBaseURL:String = Secrets.baseUrl
       
       // channelToken is rtctoken.  I should change this variable name at some point to rtctoken...
       var channelToken:String = ""
       
       lazy var rtckit: AgoraRtcEngineKit = {
           let engine = AgoraRtcEngineKit.sharedEngine(
            withAppId: Secrets.agoraAppId, delegate: self
           )
           engine.setChannelProfile(.liveBroadcasting)
           engine.setClientRole(.audience)
           return engine
       }()
    
       
   }

   extension AgoraObservable {
       
       func checkIfChannelTokenExists() {
           if channelToken.isEmpty {
               joinChannelWithFetch()
           } else {
               joinChannel()
           }
       }
       
       func joinChannelWithFetch() {
           
           AgoraToken.fetchToken(
                       urlBase: tokenBaseURL,
                       channelName: self.channelName,
                       uid: self.rtcId
                   ) { result in
                       switch result {
                       case .success(let tokenExists):
                           self.channelToken = tokenExists
                           print("func joinChannelWithFetch(): channelToken = \(self.channelToken) and rtcuid = \(self.rtcId)")
                           self.joinChannel()
                       case .failure(let err):
                           print(err)
                           // To Do: Handle this error with an alert
                           self.leaveChannel()
                           }
                       }
       }
       
       func joinChannel(){
           print("func joinChannel(): channelToken = \(self.channelToken) and channelName = \(self.channelName)  and rtcuid = \(self.rtcId)")

           self.rtckit.joinChannel(byToken: self.channelToken, channelId: self.channelName, info: nil, uid: self.rtcId) { [weak self] (channel, uid, errCode) in
               print("within rtckit.joinchannel yields: channel:\(channel) and uid:\(uid) and error:\(errCode)")
               
               self?.rtcId = uid
               
           }
           // I need to error handle if user cannot loginto rtckit
       }
       
       func updateToken(_ newToken:String){
           channelToken = newToken
           self.rtckit.renewToken(newToken)
           print("Updating token now...")
       }
       
       func leaveChannel() {
           self.rtckit.leaveChannel()
       }
       
  }

extension AgoraObservable: AgoraRtcEngineDelegate {
    /// Called when the user role successfully changes
       /// - Parameters:
       ///   - engine: AgoraRtcEngine of this session.
       ///   - oldRole: Previous role of the user.
       ///   - newRole: New role of the user.
       func rtcEngine(
           _ engine: AgoraRtcEngineKit,
           didClientRoleChanged oldRole: AgoraClientRole,
           newRole: AgoraClientRole
       ) {
           
           print("AgoraRtcEngineDelegate didClientRoleChanged triggered...old role: \(oldRole), new role: \(newRole)")

       }

       func rtcEngine(
           _ engine: AgoraRtcEngineKit,
           didJoinedOfUid uid: UInt,
           elapsed: Int
       ) {
           // Keeping track of all people in the session
           print("rtcEngine didJoinedOfUid triggered...")
           remoteUserIDs.insert(uid)
       }

       func rtcEngine(
           _ engine: AgoraRtcEngineKit,
           didOfflineOfUid uid: UInt,
           reason: AgoraUserOfflineReason
       ) {
           print("rtcEngine didOfflineOfUid triggered...")
           // Removing on quit and dropped only
           // the other option is `.becomeAudience`,
           // which means it's still relevant.
           if reason == .quit || reason == .dropped {
               remoteUserIDs.remove(uid)
           } else {
               // User is no longer hosting, need to change the lookups
               // and remove this view from the list
               // userVideoLookup.removeValue(forKey: uid)
           }
       }

       func rtcEngine(
           _ engine: AgoraRtcEngineKit,
           tokenPrivilegeWillExpire token: String
       ) {
           print("tokenPrivilegeWillExpire delegate method called...")
           AgoraToken.fetchToken(
            urlBase: tokenBaseURL, channelName: self.channelName, uid: self.rtcId) { result in
               switch result {
               case .failure(let err):
                   fatalError("Could not refresh token: \(err)")
               case .success(let newToken):
                   print("token successfully updated")
                   self.updateToken(newToken)
               }
           }
       }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
    }
}

事实证明,我在尝试为我的 http 请求定义 'fullURL' 以获取令牌时犯了一个错误。我是 http 请求的新手,所以我不知道我犯了一个错误。

在我的 AgoraToken.swift 文件中,我错误的完整 URL 定义是:

        guard let fullURL = URL(string: "\(urlBase)/?channelName=\(channelName)/&uid=\(uid)/") else {...

因为如果您不是像我这样的菜鸟,您将能够看到此代码 channelName 和 uid 都将以尾部斜线发送到我的令牌服务器。因此,如果我的 channelName 是 'TupperwareParty',我的令牌服务器将获得 'TupperwareParty/',如果我的 uid 是“123456”,我的令牌服务器将获得“123456/”。

我用这个新的完整 URL 定义修复了它...

        guard let fullURL = URL(string: "\(urlBase)/?channelName=\(channelName)&uid=\(uid)") else {...

唉...