Kommentar
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Identitetsbiblioteket används för att hantera användare och tokens för Azure Communication Services.
Komma igång
Prerequisites
- En prenumeration för Azure.
- En befintlig Communication Services-resurs. Om du behöver skapa resursen kan du använda Azure Portal, Azure PowerShelleller Azure CLI-.
Installera
npm install @azure/communication-identity
Webbläsarstöd
JavaScript-paket
Om du vill använda det här klientbiblioteket i webbläsaren måste du först använda en bundler. Mer information om hur du gör detta finns i vår paketeringsdokumentation.
Viktiga begrepp
Clients
Den CommunicationIdentityClient tillhandahåller metoder för att hantera användare och deras tokens.
Examples
Authentication
Du kan hämta en nyckel och/eller reťazec pripojenia från din Communication Services-resurs i Azure Portal. När du har en nyckel kan du autentisera den CommunicationIdentityClient med någon av följande metoder:
Skapa KeyCredential med AzureKeyCredential innan du initierar klienten
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);
Använda en anslutningssträng
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);
Med hjälp av en 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);
Om du använder en nyckel för att initiera klienten måste du också ange rätt slutpunkt. Du kan hämta den här slutpunkten från din Communication Services-resurs i Azure Portal.
Usage
Skapa en ny användare
Använd createUser metoden för att skapa en ny användare.
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();
Skapa och uppdatera en användartoken
Använd getToken metoden för att utfärda eller uppdatera en token för en befintlig användare. Metoden inkluderar också en lista över kommunikationstoken-omfattningar. Omfattningsalternativ inkluderar:
-
chat(Använd detta för full tillgång till chatt-API:er) -
voip(Använd detta för full åtkomst till anropande API:er) -
chat.join(Tillgång till chatt-API:er men utan behörighet att skapa, ta bort eller uppdatera chatttrådar) -
chat.join.limited(En mer begränsad version av chat.join som inte tillåter att lägga till eller ta bort deltagare) -
voip.join(Tillgång till anropande API:er men utan behörighet att starta nya anrop)
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"]);
För att uppdatera användartoken, utfärda en ny token med samma användare.
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"]));
Skapa en användartoken med anpassad utgångspunkt
Det är också möjligt att skapa en kommunikationsidentitetsåtkomsttoken genom att anpassa utgångstiden. Tokens giltighetstid måste vara inom intervallet [60,1440] minuter. Om det inte anges kommer standardvärdet 1440 minuter (24 timmar) att användas.
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);
Skapa en användare och en token i en enda förfrågan
För enkelhets skull, använd createUserAndToken för att skapa en ny användare och utfärda en token med ett funktionsanrop. Detta innebär en enda webbförfrågan istället för att skapa en användare först och sedan utfärda en token.
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"]);
Skapa en användare och en token med anpassad utgångspunkt i en enda förfrågan
Det är också möjligt att skapa en kommunikationsidentitetsåtkomsttoken genom att anpassa utgångstiden. Tokens giltighetstid måste vara inom intervallet [60,1440] minuter. Om det inte anges kommer standardvärdet 1440 minuter (24 timmar) att användas.
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);
Att återkalla tokens för en användare
Använd revokeTokens metoden för att återkalla alla utfärdade tokens för en användare.
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);
Att radera en användare
Använd deleteUser metoden för att ta bort en användare.
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);
Byta Azure AD-åtkomsttoken från en Teams-användare mot en kommunikationsåtkomsttoken
Använd getTokenForTeamsUser metoden för att byta en Azure AD-åtkomsttoken för en Teams-användare mot en ny CommunicationAccessToken med matchande utgångstid.
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>",
});
Troubleshooting
Logging
Aktivering av loggning kan hjälpa dig att hitta användbar information om fel. Om du vill se en logg med HTTP-begäranden och svar anger du AZURE_LOG_LEVEL miljövariabeln till info. Du kan också aktivera loggning vid körning genom att anropa setLogLevel i @azure/logger:
import { setLogLevel } from "@azure/logger";
setLogLevel("info");
Nästa steg
Ta en titt på exempelkatalogen för detaljerade exempel på hur du använder det här biblioteket.
Contributing
Om du vill bidra till detta bibliotek, läs gärna guiden bidrag för att lära dig mer om hur man bygger och testar koden.
Relaterade projekt
Azure SDK for JavaScript