Created
March 30, 2024 08:32
-
-
Save gregjlee/972fd3ba9f279fa1732992fedbc60fc2 to your computer and use it in GitHub Desktop.
Agora Token Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'bundler/inline' | |
require 'securerandom' | |
require 'json' | |
require 'time' | |
require 'base64' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'agora_dynamic_key' | |
gem 'dotenv' | |
gem 'pry-byebug' | |
end | |
require 'dynamic_key' | |
Dotenv.load | |
class Agora | |
EXPIRATION_TIME_IN_MINUTES = 60*3 # 3 hours | |
def generate_rtc_token(channel_name = 'any_channel_name', uid = 0, expiration_in_minutes = EXPIRATION_TIME_IN_MINUTES) | |
if ENV['AGORA_APP_ID'].nil? || ENV['AGORA_APP_CERTIFICATE'].nil? | |
raise 'Please add an .env file with AGORA_APP_ID and AGORA_APP_CERTIFICATE' | |
end | |
params = { | |
app_id: ENV['AGORA_APP_ID'], | |
app_certificate: ENV['AGORA_APP_CERTIFICATE'], | |
channel_name: channel_name, | |
uid: uid, # 0 is special uid with more authentication flexibility, not recommended for production | |
role: AgoraDynamicKey::RTCTokenBuilder::Role::PUBLISHER, | |
privilege_expired_ts: Time.now.to_i + expiration_in_minutes *60 | |
} | |
AgoraDynamicKey::RTCTokenBuilder.build_token_with_uid(params) | |
end | |
end | |
agora=Agora.new | |
channel_name = 'test-channel' | |
uid = 'vis:12345:123455666' | |
token = agora.generate_rtc_token('test-string-int', 'emp', 60 * 6) | |
puts "Token: #{token},\nChannel: #{channel_name},\nUID: #{uid}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment