关于 azure-communication-common-java
为 Azure Communication Services 提供通用身份验证工具和标识符类型的 Java 库,支持令牌凭据、刷新配置,以及 ACS 用户、电话号码和 Microsoft Teams 用户标识。
主要功能
- 01创建通信令牌凭据
- 02配置主动令牌刷新
- 03表示 ACS 和 Teams 用户
- 04表示电话号码和未知标识符
- 05选择通信云环境
适用场景
- 01为 ACS Chat 或 Calling 客户端进行身份验证
- 02为长期运行的客户端刷新令牌
- 03识别 ACS 用户和电话参与者
- 04处理类型化通信标识符
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-communication-common-java安装后在 Claude Code 中输入/azure-communication-common-java确认已加载。
详细介绍
原文在翻译后有更新,译文可能不是最新。
Azure Communication Common (Java)
Azure Communication Services 的共享身份验证实用工具和数据结构。
安装
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-communication-common</artifactId>
<version>1.4.0</version>
</dependency>
关键概念
| 类 | 用途 |
|---|---|
CommunicationTokenCredential | 使用 ACS 服务对用户进行身份验证 |
CommunicationTokenRefreshOptions | 配置自动令牌刷新 |
CommunicationUserIdentifier | 标识 ACS 用户 |
PhoneNumberIdentifier | 标识 PSTN 电话号码 |
MicrosoftTeamsUserIdentifier | 标识 Teams 用户 |
UnknownIdentifier | 未知类型的通用标识符 |
CommunicationTokenCredential
静态令牌(短生命周期客户端)
import com.azure.communication.common.CommunicationTokenCredential;
// Simple static token - no refresh
String userToken = "<user-access-token>";
CommunicationTokenCredential credential = new CommunicationTokenCredential(userToken);
// Use with Chat, Calling, etc.
ChatClient chatClient = new ChatClientBuilder()
.endpoint("https://<resource>.communication.azure.com")
.credential(credential)
.buildClient();
主动令牌刷新(长生命周期客户端)
import com.azure.communication.common.CommunicationTokenRefreshOptions;
import java.util.concurrent.Callable;
// Token refresher callback - called when token is about to expire
Callable<String> tokenRefresher = () -> {
// Call your server to get a fresh token
return fetchNewTokenFromServer();
};
// With proactive refresh
CommunicationTokenRefreshOptions refreshOptions = new CommunicationTokenRefreshOptions(tokenRefresher)
.setRefreshProactively(true) // Refresh before expiry
.setInitialToken(currentToken); // Optional initial token
CommunicationTokenCredential credential = new CommunicationTokenCredential(refreshOptions);
异步令牌刷新
import java.util.concurrent.CompletableFuture;
// Async token fetcher
Callable<String> asyncRefresher = () -> {
CompletableFuture<String> future = fetchTokenAsync();
return future.get(); // Block until token is available
};
CommunicationTokenRefreshOptions options = new CommunicationTokenRefreshOptions(asyncRefresher)
.setRefreshProactively(true);
CommunicationTokenCredential credential = new CommunicationTokenCredential(options);
Entra ID (Azure AD) 身份验证
import com.azure.identity.InteractiveBrowserCredentialBuilder;
import com.azure.communication.common.EntraCommunicationTokenCredentialOptions;
import java.util.Arrays;
import java.util.List;
// For Teams Phone Extensibility
InteractiveBrowserCredential entraCredential = new InteractiveBrowserCredentialBuilder()
.clientId("<your-client-id>")
.tenantId("<your-tenant-id>")
.redirectUrl("<your-redirect-uri>")
.build();
String resourceEndpoint = "https://<resource>.communication.azure.com";
List<String> scopes = Arrays.asList(
"https://auth.msft.communication.azure.com/TeamsExtension.ManageCalls"
);
EntraCommunicationTokenCredentialOptions entraOptions =
new EntraCommunicationTokenCredentialOptions(entraCredential, resourceEndpoint)
.setScopes(scopes);
CommunicationTokenCredential credential = new CommunicationTokenCredential(entraOptions);
Communication 标识符
CommunicationUserIdentifier
import com.azure.communication.common.CommunicationUserIdentifier;
// Create identifier for ACS user
CommunicationUserIdentifier user = new CommunicationUserIdentifier("8:acs:resource-id_user-id");
// Get raw ID
String rawId = user.getId();
PhoneNumberIdentifier
import com.azure.communication.common.PhoneNumberIdentifier;
// E.164 format phone number
PhoneNumberIdentifier phone = new PhoneNumberIdentifier("+14255551234");
String phoneNumber = phone.getPhoneNumber(); // "+14255551234"
String rawId = phone.getRawId(); // "4:+14255551234"
MicrosoftTeamsUserIdentifier
import com.azure.communication.common.MicrosoftTeamsUserIdentifier;
// Teams user identifier
MicrosoftTeamsUserIdentifier teamsUser = new MicrosoftTeamsUserIdentifier("<teams-user-id>")
.setCloudEnvironment(CommunicationCloudEnvironment.PUBLIC);
// For anonymous Teams users
MicrosoftTeamsUserIdentifier anonymousTeamsUser = new MicrosoftTeamsUserIdentifier("<teams-user-id>")
.setAnonymous(true);
UnknownIdentifier
import com.azure.communication.common.UnknownIdentifier;
// For identifiers of unknown type
UnknownIdentifier unknown = new UnknownIdentifier("some-raw-id");
标识符解析
import com.azure.communication.common.CommunicationIdentifier;
import com.azure.communication.common.CommunicationIdentifierModel;
// Parse raw ID to appropriate type
public CommunicationIdentifier parseIdentifier(String rawId) {
if (rawId.startsWith("8:acs:")) {
return new CommunicationUserIdentifier(rawId);
} else if (rawId.startsWith("4:")) {
String phone = rawId.substring(2);
return new PhoneNumberIdentifier(phone);
} else if (rawId.startsWith("8:orgid:")) {
String teamsId = rawId.substring(8);
return new MicrosoftTeamsUserIdentifier(teamsId);
} else {
return new UnknownIdentifier(rawId);
}
}
类型检查标识符
import com.azure.communication.common.CommunicationIdentifier;
public void processIdentifier(CommunicationIdentifier identifier) {
if (identifier instanceof CommunicationUserIdentifier) {
CommunicationUserIdentifier user = (CommunicationUserIdentifier) identifier;
System.out.println("ACS User: " + user.getId());
} else if (identifier instanceof PhoneNumberIdentifier) {
PhoneNumberIdentifier phone = (PhoneNumberIdentifier) identifier;
System.out.println("Phone: " + phone.getPhoneNumber());
} else if (identifier instanceof MicrosoftTeamsUserIdentifier) {
MicrosoftTeamsUserIdentifier teams = (MicrosoftTeamsUserIdentifier) identifier;
System.out.println("Teams User: " + teams.getUserId());
System.out.println("Anonymous: " + teams.isAnonymous());
} else if (identifier instanceof UnknownIdentifier) {
UnknownIdentifier unknown = (UnknownIdentifier) identifier;
System.out.println("Unknown: " + unknown.getId());
}
}
令牌访问
import com.azure.core.credential.AccessToken;
// Get current token (for debugging/logging - don't expose!)
CommunicationTokenCredential credential = new CommunicationTokenCredential(token);
// Sync access
AccessToken accessToken = credential.getToken();
System.out.println("Token expires: " + accessToken.getExpiresAt());
// Async access
credential.getTokenAsync()
.subscribe(token -> {
System.out.println("Token: " + token.getToken().substring(0, 20) + "...");
System.out.println("Expires: " + token.getExpiresAt());
});
释放凭据
// Clean up when done
credential.close();
// Or use try-with-resources
try (CommunicationTokenCredential cred = new CommunicationTokenCredential(options)) {
// Use credential
chatClient.doSomething();
}
云环境
import com.azure.communication.common.CommunicationCloudEnvironment;
// Available environments
CommunicationCloudEnvironment publicCloud = CommunicationCloudEnvironment.PUBLIC;
CommunicationCloudEnvironment govCloud = CommunicationCloudEnvironment.GCCH;
CommunicationCloudEnvironment dodCloud = CommunicationCloudEnvironment.DOD;
// Set on Teams identifier
MicrosoftTeamsUserIdentifier teamsUser = new MicrosoftTeamsUserIdentifier("<user-id>")
.setCloudEnvironment(CommunicationCloudEnvironment.GCCH);
环境变量
AZURE_COMMUNICATION_ENDPOINT=https://<resource>.communication.azure.com
AZURE_COMMUNICATION_USER_TOKEN=<user-access-token>
最佳实践
- 主动刷新 - 对于长生命周期客户端,始终使用
setRefreshProactively(true) - 令牌安全 - 切勿记录或暴露完整令牌
- 关闭凭据 - 不再需要时释放凭据
- 错误处理 - 妥善处理令牌刷新失败
- 标识符类型 - 使用特定的标识符类型,而不是原始字符串
常见使用模式
// Pattern: Create credential for Chat/Calling client
public ChatClient createChatClient(String token, String endpoint) {
CommunicationTokenRefreshOptions refreshOptions =
new CommunicationTokenRefreshOptions(this::refreshToken)
.setRefreshProactively(true)
.setInitialToken(token);
CommunicationTokenCredential credential =
new CommunicationTokenCredential(refreshOptions);
return new ChatClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
}
private String refreshToken() {
// Call your token endpoint
return tokenService.getNewToken();
}
触发短语
- "ACS authentication"、"communication token credential"
- "user access token"、"token refresh"
- "CommunicationUserIdentifier"、"PhoneNumberIdentifier"
- "Azure Communication Services authentication"
何时使用
此技能适用于执行概述中描述的工作流或操作。
限制
- 仅当任务与上述范围明确匹配时才使用此技能。
- 不要将输出视为特定环境验证、测试或专家评审的替代品。
- 如果缺少必需的输入、权限、安全边界或成功标准,请停止并请求澄清。
评论
支持 Markdown