Skip to main content

How can I generate a token with both RTC and Signaling privileges?

Agora provides source code in several languages to generate tokens with both RTC and Signaling privileges. Refer to the following samples to generate tokens on your own server:

#include <cstdlib>
#include <iostream>

#include "../src/RtcTokenBuilder2.h"

using namespace agora::tools;

int main(int argc, char const *argv[]) {
(void)argc;
(void)argv;

// Get the value of the environment variable AGORA_APP_ID. Make sure you set this to the App ID from the Agora Console.
const char *env_app_id = getenv("AGORA_APP_ID");
std::string app_id = env_app_id ? env_app_id : "";
// Get the value of the environment variable AGORA_APP_CERTIFICATE. Make sure you set this to the App Certificate from the Agora Console.
const char *env_app_certificate = getenv("AGORA_APP_CERTIFICATE");
std::string app_certificate = env_app_certificate ? env_app_certificate : "";
// Replace channelName with the name of the channel to join.
std::string channel_name = "channelName";
// User ID in string format.
std::string account = "account";
// Token expiration time in seconds.
uint32_t token_expiration_in_seconds = 3600;
// Privilege expiration time in seconds. Agora recommends setting this equal to the token expiration time.
uint32_t privilege_expiration_in_seconds = 3600;
std::string result;

std::cout << "App Id:" << app_id << std::endl;
std::cout << "App Certificate:" << app_certificate << std::endl;
if (app_id == "" || app_certificate == "") {
std::cout << "Need to set environment variable AGORA_APP_ID and "
"AGORA_APP_CERTIFICATE"
<< std::endl;
return -1;
}
// Generate a token with both RTC and RTM privileges.
result = RtcTokenBuilder2::BuildTokenWithRtm(app_id, app_certificate, channel_name, account, UserRole::kRolePublisher, token_expiration_in_seconds,
privilege_expiration_in_seconds);
std::cout << "Token With RTM:" << result << std::endl;

return 0;
}