Azure Communication Services common utilities for Java. Use when working with CommunicationTokenCredential, user identifiers, token refresh, or shared authentication across ACS services.
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
A direct command skips the review prompt. Inspect the source before running it.
Azure Communication Services common utilities for Java. Use when working with CommunicationTokenCredential, user identifiers, token refresh, or shared authentication across ACS services.
import com.azure.communication.common.CommunicationTokenCredential;
// Simple static token - no refreshStringuserToken="<user-access-token>";
CommunicationTokenCredentialcredential=newCommunicationTokenCredential(userToken);
// Use with Chat, Calling, etc.ChatClient ()
.endpoint()
.credential(credential)
.buildClient();
chatClient
=
new
ChatClientBuilder
"https://<resource>.communication.azure.com"
Proactive Token Refresh (Long-lived Clients)
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 tokenreturn fetchNewTokenFromServer();
};
// With proactive refreshCommunicationTokenRefreshOptionsrefreshOptions=newCommunicationTokenRefreshOptions(tokenRefresher)
.setRefreshProactively(true) // Refresh before expiry
.setInitialToken(currentToken); // Optional initial tokenCommunicationTokenCredentialcredential=newCommunicationTokenCredential(refreshOptions);
Async Token Refresh
import java.util.concurrent.CompletableFuture;
// Async token fetcher
Callable<String> asyncRefresher = () -> {
CompletableFuture<String> future = fetchTokenAsync();
return future.get(); // Block until token is available
};
CommunicationTokenRefreshOptionsoptions=newCommunicationTokenRefreshOptions(asyncRefresher)
.setRefreshProactively(true);
CommunicationTokenCredentialcredential=newCommunicationTokenCredential(options);
import com.azure.communication.common.CommunicationUserIdentifier;
// Create identifier for ACS userCommunicationUserIdentifieruser=newCommunicationUserIdentifier("8:acs:resource-id_user-id");
// Get raw IDStringrawId= user.getId();
import com.azure.communication.common.MicrosoftTeamsUserIdentifier;
// Teams user identifierMicrosoftTeamsUserIdentifierteamsUser=newMicrosoftTeamsUserIdentifier("<teams-user-id>")
.setCloudEnvironment(CommunicationCloudEnvironment.PUBLIC);
// For anonymous Teams usersMicrosoftTeamsUserIdentifieranonymousTeamsUser=newMicrosoftTeamsUserIdentifier("<teams-user-id>")
.setAnonymous(true);
UnknownIdentifier
import com.azure.communication.common.UnknownIdentifier;
// For identifiers of unknown typeUnknownIdentifierunknown=newUnknownIdentifier("some-raw-id");
Identifier Parsing
import com.azure.communication.common.CommunicationIdentifier;
import com.azure.communication.common.CommunicationIdentifierModel;
// Parse raw ID to appropriate typepublic CommunicationIdentifier parseIdentifier(String rawId) {
if (rawId.startsWith("8:acs:")) {
returnnewCommunicationUserIdentifier(rawId);
} elseif (rawId.startsWith("4:")) {
Stringphone= rawId.substring(2);
returnnewPhoneNumberIdentifier(phone);
} elseif (rawId.startsWith("8:orgid:")) {
StringteamsId= rawId.substring(8);
returnnewMicrosoftTeamsUserIdentifier(teamsId);
} else {
returnnewUnknownIdentifier(rawId);
}
}
// Clean up when done
credential.close();
// Or use try-with-resourcestry (CommunicationTokenCredentialcred=newCommunicationTokenCredential(options)) {
// Use credential
chatClient.doSomething();
}
Cloud Environments
import com.azure.communication.common.CommunicationCloudEnvironment;
// Available environmentsCommunicationCloudEnvironmentpublicCloud= CommunicationCloudEnvironment.PUBLIC;
CommunicationCloudEnvironmentgovCloud= CommunicationCloudEnvironment.GCCH;
CommunicationCloudEnvironmentdodCloud= CommunicationCloudEnvironment.DOD;
// Set on Teams identifierMicrosoftTeamsUserIdentifierteamsUser=newMicrosoftTeamsUserIdentifier("<user-id>")
.setCloudEnvironment(CommunicationCloudEnvironment.GCCH);