|
| 1 | +# Agora Token Generator |
| 2 | + |
| 3 | +A Flutter package for generating Agora Dynamic Keys and Access Tokens to use with Agora.io SDKs. This package provides a pure Dart implementation of Agora's token generation mechanism. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Generate RTC tokens for Agora Real-Time Communications |
| 8 | +- Generate RTM tokens for Agora Real-Time Messaging |
| 9 | +- Support for the latest token format (AccessToken2 - 007) |
| 10 | +- No native dependencies |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Add this to your package's `pubspec.yaml` file: |
| 15 | + |
| 16 | +```yaml |
| 17 | +dependencies: |
| 18 | + agora_token_generator: ^0.0.1 |
| 19 | +``` |
| 20 | +
|
| 21 | +Then run: |
| 22 | +
|
| 23 | +```bash |
| 24 | +flutter pub get |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### RTC Token |
| 30 | + |
| 31 | +Generate a token for Agora RTC service with a numeric user ID: |
| 32 | + |
| 33 | +```dart |
| 34 | +import 'package:agora_token_generator/agora_token_generator.dart'; |
| 35 | +
|
| 36 | +void main() { |
| 37 | + String appId = 'YOUR_APP_ID'; |
| 38 | + String appCertificate = 'YOUR_APP_CERTIFICATE'; |
| 39 | + String channelName = 'test_channel'; |
| 40 | + int uid = 12345; |
| 41 | + int tokenExpireSeconds = 3600; // Token expires in 1 hour |
| 42 | +
|
| 43 | + String token = RtcTokenBuilder.buildTokenWithUid( |
| 44 | + appId: appId, |
| 45 | + appCertificate: appCertificate, |
| 46 | + channelName: channelName, |
| 47 | + uid: uid, |
| 48 | + tokenExpireSeconds: tokenExpireSeconds, |
| 49 | + ); |
| 50 | +
|
| 51 | + print('RTC Token: $token'); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Generate a token for Agora RTC service with a string user account: |
| 56 | + |
| 57 | +```dart |
| 58 | +import 'package:agora_token_generator/agora_token_generator.dart'; |
| 59 | +
|
| 60 | +void main() { |
| 61 | + String appId = 'YOUR_APP_ID'; |
| 62 | + String appCertificate = 'YOUR_APP_CERTIFICATE'; |
| 63 | + String channelName = 'test_channel'; |
| 64 | + String userAccount = 'user123'; |
| 65 | + int tokenExpireSeconds = 3600; // Token expires in 1 hour |
| 66 | +
|
| 67 | + String token = RtcTokenBuilder.buildTokenWithAccount( |
| 68 | + appId: appId, |
| 69 | + appCertificate: appCertificate, |
| 70 | + channelName: channelName, |
| 71 | + account: userAccount, |
| 72 | + tokenExpireSeconds: tokenExpireSeconds, |
| 73 | + ); |
| 74 | +
|
| 75 | + print('RTC Token with User Account: $token'); |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### RTM Token |
| 80 | + |
| 81 | +Generate a token for Agora RTM service: |
| 82 | + |
| 83 | +```dart |
| 84 | +import 'package:agora_token_generator/agora_token_generator.dart'; |
| 85 | +
|
| 86 | +void main() { |
| 87 | + String appId = 'YOUR_APP_ID'; |
| 88 | + String appCertificate = 'YOUR_APP_CERTIFICATE'; |
| 89 | + String userId = 'user123'; |
| 90 | + int tokenExpireSeconds = 3600; // Token expires in 1 hour |
| 91 | +
|
| 92 | + String token = RtmTokenBuilder.buildToken( |
| 93 | + appId: appId, |
| 94 | + appCertificate: appCertificate, |
| 95 | + userId: userId, |
| 96 | + tokenExpireSeconds: tokenExpireSeconds, |
| 97 | + ); |
| 98 | +
|
| 99 | + print('RTM Token: $token'); |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Getting Agora App ID and App Certificate |
| 104 | + |
| 105 | +To use this token generator, you need to get an App ID and App Certificate from Agora: |
| 106 | + |
| 107 | +1. Create an account at [https://console.agora.io/](https://console.agora.io/) |
| 108 | +2. Create a new project |
| 109 | +3. Get the App ID from the project dashboard |
| 110 | +4. Generate an App Certificate for your project (Keep it safe and don't share it!) |
| 111 | + |
| 112 | +## About Agora Token Authentication |
| 113 | + |
| 114 | +Agora uses token-based authentication to secure connections to their services. This package implements the latest token format (AccessToken2 - 007). |
| 115 | + |
| 116 | +## License |
| 117 | + |
| 118 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 119 | + |
| 120 | +## Acknowledgments |
| 121 | + |
| 122 | +This Flutter/Dart implementation is based on Agora's official authentication mechanism as demonstrated in their SDKs for other languages. |
0 commit comments