在 Android Studio 应用程序中生成 Azure 通信服务令牌

问题描述 投票:0回答:1

我正在尝试遵循 Microsoft Learn 上的 Azure 快速入门,但是当我到达有关创建 用户访问令牌 的部分时,我无法按照 Java 指南生成令牌。

这是我试图实现的代码:

private void createAgent() {
    String connectionString = "<String connectionString = \"<endpoint=https://agentq-1984.unitedstates.communication.azure.com/;accesskey=5sq2CpfGlkGb46AohKEMaBaGTMEhOUKAQnq3oViNdcS0FzYAOC9RJQQJ99AJACULyCpFGRGZAAAAAZCScinz>\";>";
    CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClientBuilder().connectionString(connectionString).buildClient();
    List<CommunicationTokenScope> scopes = Arrays.asList(CommunicationTokenScope.VOIP);
    CommunicationUserIdentifierAndToken result = communicationIdentityClient.createUserAndToken(scopes);
    CommunicationUserIdentifier user = result.getUser();
    AccessToken accessToken = result.getUserToken();
    OffsetDateTime expiresAt = accessToken.getExpiresAt();

    Context context = this.getApplicationContext();
    String userToken = accessToken.getToken();
    try {
        CommunicationTokenCredential credential = new CommunicationTokenCredential(userToken);
        CallClient callClient = new CallClient();
        deviceManager = callClient.getDeviceManager(context).get();
        callAgent = callClient.createCallAgent(getApplicationContext(), credential).get();
    } catch (Exception ex) {
        Toast.makeText(context, "Failed to create call agent.", Toast.LENGTH_SHORT).show();
    }
}

我遇到了从“CommunicationIdentityClient”开始的问题 - 它(和其他内容)显示为红色,并显示“无法解析”错误消息。似乎有很多关于为 Java 或 Windows 应用程序设置令牌等的信息,但我找不到为 Android 应用程序设置令牌的答案。我尝试导入 Azure 通信服务 Android SDK,但没有成功。

我错过了图书馆吗?我缺课了吗?我还漏掉了什么吗?

azure android-studio azure-communication-services
1个回答
0
投票

这是我拼凑的解决方案:

在“build.gradle”中:

implementation libs.azure.communication.identity

在“主要活动”中:

CommunicationIdentityClient communicationsIdentityClient = new com.azure.communication.identity.CommunicationIdentityClientBuilder().connectionString(connectionString).buildClient();

    com.azure.communication.common.CommunicationUserIdentifier user = communicationIdentityClient.createUser();

    List<CommunicationTokenScope> scopes = new ArrayList<>(Arrays.asList(CommunicationTokenScope.VOIP));
    AccessToken accessToken = communicationIdentityClient.getToken(user, scopes);
    OffsetDateTime expiresAt = accessToken.getExpiresAt();

    Context context = this.getApplicationContext();
    String userToken = accessToken.getToken();
© www.soinside.com 2019 - 2024. All rights reserved.