JavaScript 的 Azure 通讯身份客户端库
身份库用于管理Azure通信服务的用户和信使.
开始
先决条件
安装
npm install @azure/communication-identity
浏览器支持
Java 脚本套装
要在浏览器中使用此客户端库,首先需要使用捆绑器. 关于如何做到这一点的详情,请参见我们 捆绑文件。 。 。 .
关键概念
客户
这个 CommunicationIdentityClient 提供管理用户及其代币的方法.
实例
认证
您可以从您的通信服务资源中获取密钥和/或连接字符串 Azure 门户。一旦你有钥匙,就可以认证 CommunicationIdentityClient 采用下列任何一种方法:
创建 KeyCredential 与 AzureKeyCredential 在初始化客户端之前
import { AzureKeyCredential } from "@azure/core-auth";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const key = "<some-key>";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new AzureKeyCredential(key);
const client = new CommunicationIdentityClient(endpoint, credential);
使用连接字符串
import { CommunicationIdentityClient } from "@azure/communication-identity";
// Example connection string
const connectionString =
"endpoint=https://contoso.eastus.communications.azure.net/;accesskey=secret";
const client = new CommunicationIdentityClient(connectionString);
使用a TokenCredential
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
如果您使用密钥初始化客户端, 您还需要提供合适的端点 。 您可以从您的通信服务资源中获取此端点 Azure 门户。 。 。 .
使用量
创建新用户
使用 createUser 创建新用户的方法.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const user = await client.createUser();
创建和刷新用户令牌
使用 getToken 为已有用户发布或刷新一个符号的方法。 该方法还收录在通信符号范围列表中. 范围选项包括:
chat(用这个来全程访问Chat API)voip(这个可以全程访问API)chat.join(访问 Chat API 但无权创建、删除或更新聊天线索)chat.join.limited(一个比较有限的聊天版本.join,不允许添加或删除参与者)voip.join(进入API但无权开始新电话)
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const user = await client.createUser();
const { token } = await client.getToken(user, ["chat"]);
要刷新用户令牌,请与同一用户再发一个令牌.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const user = await client.createUser();
let { token } = await client.getToken(user, ["chat"]);
// Refresh the token again
({ token } = await client.getToken(user, ["chat"]));
创建自定义过期的用户令牌
也可以通过自定义过期时间来创建通信身份访问符. 令牌的有效期必须在[60,1440]分以内. 如果不提供,将使用1440分(24小时)的默认值.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const user = await client.createUser();
const tokenOptions = { tokenExpiresInMinutes: 60 };
const { token } = await client.getToken(user, ["chat"], tokenOptions);
在单个请求中创建用户和令牌
为方便起见 createUserAndToken 创建新用户,并发布一个函数调用符。 这可以翻译成一个单一的网络请求,而不是先创建用户,再发行一个令牌.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const { user, token } = await client.createUserAndToken(["chat"]);
在单个请求中创建用户和自定义过期的令牌
也可以通过自定义过期时间来创建通信身份访问符. 令牌的有效期必须在[60,1440]分以内. 如果不提供,将使用1440分(24小时)的默认值.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const userAndTokenOptions = { tokenExpiresInMinutes: 60 };
const { user, token } = await client.createUserAndToken(["chat"], userAndTokenOptions);
为用户撤销令牌
使用 revokeTokens 为用户撤销所有已签发的令牌的方法.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
// Create user
const user = await client.createUser();
// Later when you want to revoke the user's tokens
await client.revokeTokens(user);
删除用户
使用 deleteUser 删除用户的方法.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
// Create user
const user = await client.createUser();
// Later when you want to delete the user
await client.deleteUser(user);
交换 Teams 用户的 Azure AD 访问令牌用于通信访问令牌
使用 getTokenForTeamsUser 将 Teams 用户的 Azure AD 访问符换为新符的方法 CommunicationAccessToken 带有匹配的过期时间.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const { token, expiresOn } = await client.getTokenForTeamsUser({
teamsUserAadToken: "<aad-access-token-of-a-teams-user>",
clientId: "<cliend-id-of-an-aad-application>",
userObjectId: "<aad-object-id-of-a-teams-user>",
});
解决问题
日志
允许伐木可能有助于发现有关失败的有用信息。 为了查看 HTTP 请求和回复的日志,设置 AZURE_LOG_LEVEL 环境变量改为 info。或者,可以在运行时拨打 setLogLevel 输入 @azure/logger数字 :
import { setLogLevel } from "@azure/logger";
setLogLevel("info");
今后的步骤
请看一下 样本 关于如何使用此库的详细示例的目录.
捐款
如果您想为图书馆捐钱 请读一下 贡献指南 学习如何构建和测试密码.