85 lines
3.2 KiB
Go
85 lines
3.2 KiB
Go
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"servicebase/pkg/tools"
|
|
|
|
rtctokenbuilder "github.com/AgoraIO/Tools/DynamicKey/AgoraDynamicKey/go/src/rtctokenbuilder2"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func GenerateShenWangToken(userNo, RoomId string) (token string, e error) {
|
|
// Need to set environment variable AGORA_APP_ID
|
|
appId := viper.GetString("agora.tokenGen.appId")
|
|
// Need to set environment variable AGORA_APP_CERTIFICATE
|
|
appCertificate := viper.GetString("agora.tokenGen.appCertificate")
|
|
channelName := RoomId
|
|
uidStr := userNo
|
|
uid := uint32(tools.StrToInt32(userNo))
|
|
expire := 3600 * 24
|
|
tokenExpirationInSeconds := uint32(expire)
|
|
privilegeExpirationInSeconds := uint32(expire)
|
|
joinChannelPrivilegeExpireInSeconds := uint32(expire)
|
|
pubAudioPrivilegeExpireInSeconds := uint32(expire)
|
|
pubVideoPrivilegeExpireInSeconds := uint32(expire)
|
|
pubDataStreamPrivilegeExpireInSeconds := uint32(expire)
|
|
|
|
fmt.Println("App Id:", appId)
|
|
fmt.Println("App Certificate:", appCertificate)
|
|
if appId == "" || appCertificate == "" {
|
|
fmt.Println("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE")
|
|
return
|
|
}
|
|
|
|
result, err := rtctokenbuilder.BuildTokenWithUid(appId, appCertificate, channelName, uid, rtctokenbuilder.RolePublisher, tokenExpirationInSeconds, privilegeExpirationInSeconds)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "", err
|
|
} else {
|
|
fmt.Printf("Token with int uid: %s\n", result)
|
|
}
|
|
|
|
result, err = rtctokenbuilder.BuildTokenWithUserAccount(appId, appCertificate, channelName, uidStr, rtctokenbuilder.RolePublisher, tokenExpirationInSeconds, privilegeExpirationInSeconds)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "", err
|
|
} else {
|
|
fmt.Printf("Token with user account: %s\n", result)
|
|
}
|
|
|
|
result, err = rtctokenbuilder.BuildTokenWithUidAndPrivilege(appId, appCertificate, channelName, uid,
|
|
tokenExpirationInSeconds, joinChannelPrivilegeExpireInSeconds, pubAudioPrivilegeExpireInSeconds, pubVideoPrivilegeExpireInSeconds, pubDataStreamPrivilegeExpireInSeconds)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "", err
|
|
} else {
|
|
fmt.Printf("Token with int uid and privilege: %s\n", result)
|
|
}
|
|
|
|
result, err = rtctokenbuilder.BuildTokenWithUserAccountAndPrivilege(appId, appCertificate, channelName, uidStr,
|
|
tokenExpirationInSeconds, joinChannelPrivilegeExpireInSeconds, pubAudioPrivilegeExpireInSeconds, pubVideoPrivilegeExpireInSeconds, pubDataStreamPrivilegeExpireInSeconds)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "", err
|
|
} else {
|
|
fmt.Printf("Token with user account and privilege: %s\n", result)
|
|
}
|
|
|
|
result, err = rtctokenbuilder.BuildTokenWithRtm(appId, appCertificate, channelName, uidStr, rtctokenbuilder.RolePublisher, tokenExpirationInSeconds, privilegeExpirationInSeconds)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "", err
|
|
} else {
|
|
fmt.Printf("Token with RTM: %s\n", result)
|
|
}
|
|
|
|
result, err = rtctokenbuilder.BuildTokenWithRtm2(appId, appCertificate, channelName, uidStr, rtctokenbuilder.RolePublisher, tokenExpirationInSeconds, privilegeExpirationInSeconds, pubAudioPrivilegeExpireInSeconds, pubVideoPrivilegeExpireInSeconds, pubDataStreamPrivilegeExpireInSeconds, uidStr, tokenExpirationInSeconds)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "", err
|
|
} else {
|
|
fmt.Printf("Token with RTM: %s\n", result)
|
|
}
|
|
return result, err
|
|
}
|