Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions DynamicKey/AgoraDynamicKey/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Sample Code for generating AccessToken are available on the following platforms:
+ C++
+ Go
+ Java
+ Kotlin
+ Node.js
+ Python
+ Python3
Expand Down Expand Up @@ -65,6 +66,11 @@ Sample Code for generating AccessToken are available on the following platforms:
+ https://github.com/AgoraIO/Tools/blob/master/DynamicKey/AgoraDynamicKey/java/src/main/java/io/agora/sample/RtcTokenBuilderSample.java
+ https://github.com/AgoraIO/Tools/blob/master/DynamicKey/AgoraDynamicKey/java/src/main/java/io/agora/sample/RtmTokenBuilderSample.java

### Kotlin

- Version 007
+ https://github.com/AgoraIO/Tools/blob/master/DynamicKey/AgoraDynamicKey/kotlin/src/sample/kotlin/io/agora/sample/RtcTokenBuilder2Sample.kt

### Node.js

- Version 007
Expand Down Expand Up @@ -359,6 +365,71 @@ public class RtcTokenBuilder2Sample {
}
```

### Kotlin
```kotlin
package io.agora.sample

import io.agora.media.RtcTokenBuilder2

object RtcTokenBuilder2Sample {
// Need to set environment variable AGORA_APP_ID
private val appId = System.getenv("AGORA_APP_ID")

// Need to set environment variable AGORA_APP_CERTIFICATE
private val appCertificate = System.getenv("AGORA_APP_CERTIFICATE")

private const val channelName = "7d72365eb983485397e3e3f9d460bdda"
private const val account = "2082341273"
private const val uid = 2082341273
private const val tokenExpirationInSeconds = 3600
private const val privilegeExpirationInSeconds = 3600
private const val joinChannelPrivilegeExpireInSeconds = 3600
private const val pubAudioPrivilegeExpireInSeconds = 3600
private const val pubVideoPrivilegeExpireInSeconds = 3600
private const val pubDataStreamPrivilegeExpireInSeconds = 3600

@JvmStatic
fun main(args: Array<String>) {
println("App Id: $appId")
println("App Certificate: $appCertificate")
if (appId == null || appId.isEmpty() || appCertificate == null || appCertificate.isEmpty()) {
println("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE")
return
}

val tokenBuilder = RtcTokenBuilder2()
var result = tokenBuilder.buildTokenWithUid(
appId, appCertificate, channelName, uid, RtcTokenBuilder2.Role.ROLE_PUBLISHER,
tokenExpirationInSeconds, privilegeExpirationInSeconds
)
println("Token with uid: $result")

result = tokenBuilder.buildTokenWithUserAccount(
appId, appCertificate, channelName, account,
RtcTokenBuilder2.Role.ROLE_PUBLISHER,
tokenExpirationInSeconds, privilegeExpirationInSeconds
)
println("Token with account: $result")

result = tokenBuilder.buildTokenWithUid(
appId, appCertificate, channelName, uid, tokenExpirationInSeconds,
joinChannelPrivilegeExpireInSeconds, pubAudioPrivilegeExpireInSeconds,
pubVideoPrivilegeExpireInSeconds,
pubDataStreamPrivilegeExpireInSeconds
)
println("Token with uid and privilege: $result")

result = tokenBuilder.buildTokenWithUserAccount(
appId, appCertificate, channelName, account,
tokenExpirationInSeconds,
joinChannelPrivilegeExpireInSeconds, pubAudioPrivilegeExpireInSeconds,
pubVideoPrivilegeExpireInSeconds, pubDataStreamPrivilegeExpireInSeconds
)
println("Token with account and privilege: $result")
}
}
```

### Node.js
```javascript
const RtcTokenBuilder = require("../src/RtcTokenBuilder2").RtcTokenBuilder;
Expand Down
60 changes: 60 additions & 0 deletions DynamicKey/AgoraDynamicKey/kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Agora Token Generator Kotlin

This project provides the implementation of Agora AccessToken2 in Kotlin.

## Project Structure

- `src/main/kotlin`: Core logic for generating tokens.
- `src/sample/kotlin`: Sample usage of the token builders.
- `src/test/kotlin`: Unit tests for the implementation.

## How to use

### Build

You can build the project using Gradle:

```bash
./gradlew build
```

### Run Sample

To run the sample, you need to set the following environment variables:
- `AGORA_APP_ID`: Your Agora App ID.
- `AGORA_APP_CERTIFICATE`: Your Agora App Certificate.

Then run:

```bash
./gradlew runSample
```

(Note: You might need to add a `runSample` task to your `build.gradle.kts` if you want to run it via Gradle)

### Run Tests

```bash
./gradlew test
```

## Example Usage

```kotlin
import io.agora.media.RtcTokenBuilder2

val appId = "YOUR_APP_ID"
val appCertificate = "YOUR_APP_CERTIFICATE"
val channelName = "YOUR_CHANNEL_NAME"
val uid = 123
val role = RtcTokenBuilder2.Role.ROLE_PUBLISHER
val tokenExpire = 3600
val privilegeExpire = 3600

val tokenBuilder = RtcTokenBuilder2()
val token = tokenBuilder.buildTokenWithUid(
appId, appCertificate, channelName, uid, role,
tokenExpire, privilegeExpire
)
println("Token: $token")
```
43 changes: 43 additions & 0 deletions DynamicKey/AgoraDynamicKey/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
plugins {
kotlin("jvm") version "1.9.0"
}

group = "io.agora"
version = "1.0.0"

repositories {
mavenCentral()
}

dependencies {
testImplementation(kotlin("test"))
testImplementation("junit:junit:4.13.2")
}

kotlin {
jvmToolchain(8)
}

sourceSets {
main {
kotlin.setSrcDirs(listOf("src/main/kotlin"))
}
test {
kotlin.setSrcDirs(listOf("src/test/kotlin"))
}
create("sample") {
kotlin.setSrcDirs(listOf("src/sample/kotlin"))
compileClasspath += sourceSets.main.get().output + sourceSets.test.get().compileClasspath
runtimeClasspath += sourceSets.main.get().output + sourceSets.test.get().runtimeClasspath
}
}

val sampleJar = task<Jar>("sampleJar") {
archiveClassifier.set("sample")
from(sourceSets["sample"].allSource)
}

tasks.register<JavaExec>("runSample") {
mainClass.set("io.agora.sample.RtcTokenBuilder2Sample")
classpath = sourceSets["sample"].runtimeClasspath
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
183 changes: 183 additions & 0 deletions DynamicKey/AgoraDynamicKey/kotlin/gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading