L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Créer un client Microsoft Graph
Le client Microsoft Graph est conçu pour simplifier les appels à Microsoft Graph. Vous pouvez utiliser un seul client instance pour la durée de vie de l’application. Pour plus d’informations sur l’ajout et l’installation du package client Microsoft Graph dans votre projet, consultez Installer le Kit de développement logiciel (SDK).
Les exemples de code suivants montrent comment créer un instance d’un client Microsoft Graph avec un fournisseur d’authentification dans les langues prises en charge. Le fournisseur d’authentification gère l’acquisition de jetons d’accès pour l’application.
De nombreux fournisseurs d’authentification différents sont disponibles pour chaque langue et chaque plateforme. Les différents fournisseurs d’authentification prennent en charge différents scénarios clients. Pour plus d’informations sur le fournisseur et les options qui conviennent à votre scénario, consultez Choisir un fournisseur d’authentification.
var scopes = new[] { "User.Read" };
// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "common";
// Value from app registration
var clientId = "YOUR_CLIENT_ID";
// using Azure.Identity;
var options = new DeviceCodeCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
ClientId = clientId,
TenantId = tenantId,
// Callback function that receives the user prompt
// Prompt contains the generated device code that user must
// enter during the auth process in the browser
DeviceCodeCallback = (code, cancellation) =>
{
Console.WriteLine(code.Message);
return Task.FromResult(0);
},
};
// https://learn.microsoft.com/dotnet/api/azure.identity.devicecodecredential
var deviceCodeCredential = new DeviceCodeCredential(options);
var graphClient = new GraphServiceClient(deviceCodeCredential, scopes);
Instructions Include using pour et Microsoft.Graph pour Azure.Identity exécuter ce code.
Incluez "github.com/Azure/azure-sdk-for-go/sdk/azidentity" et graph "github.com/microsoftgraph/msgraph-sdk-go" dans votre bloc d’importation pour exécuter ce code.
final String clientId = "YOUR_CLIENT_ID";
final String tenantId = "YOUR_TENANT_ID"; // or "common" for multi-tenant apps
final String[] scopes = new String[] {"User.Read"};
final DeviceCodeCredential credential = new DeviceCodeCredentialBuilder()
.clientId(clientId).tenantId(tenantId).challengeConsumer(challenge -> {
// Display challenge to the user
System.out.println(challenge.getMessage());
}).build();
if (null == scopes || null == credential) {
throw new Exception("Unexpected error");
}
final GraphServiceClient graphClient = new GraphServiceClient(credential, scopes);
Instructions Include import pour com.azure.identity.DeviceCodeCredential, com.azure.identity.DeviceCodeCredentialBuilderet com.microsoft.graph.serviceclient.GraphServiceClient pour exécuter ce code.
$scopes = ['User.Read'];
// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
$tenantId = 'common';
// Values from app registration
$clientId = 'YOUR_CLIENT_ID';
$clientSecret = 'YOUR_CLIENT_SECRET';
$redirectUri = 'YOUR_REDIRECT_URI';
// For authorization code flow, the user signs into the Microsoft
// identity platform, and the browser is redirected back to your app
// with an authorization code in the query parameters
$authorizationCode = 'AUTH_CODE_FROM_REDIRECT';
// Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext
$tokenContext = new AuthorizationCodeContext(
$tenantId,
$clientId,
$clientSecret,
$authorizationCode,
$redirectUri);
$graphClient = new GraphServiceClient($tokenContext, $scopes);
Instructions Include use pour Microsoft\Graph\GraphRequestAdapter, Microsoft\Graph\GraphServiceClientet Microsoft\Kiota\Abstractions\Authentication\BaseBearerTokenAuthenticationProvider pour exécuter ce code.
scopes = ['User.Read']
# Multi-tenant apps can use "common",
# single-tenant apps must use the tenant ID from the Azure portal
tenant_id = 'common'
# Values from app registration
client_id = 'YOUR_CLIENT_ID'
# azure.identity
credential = DeviceCodeCredential(
tenant_id=tenant_id,
client_id=client_id)
graph_client = GraphServiceClient(credential, scopes)
Include import instructions for DeviceCodeCredential from azure.identity et GraphServiceClient from msgraph.graph_service_client pour exécuter ce code.
Les instructions Include import pour DeviceCodeCredential à partir de @azure/identity, Client à partir de @microsoft/microsoft-graph-clientet TokenCredentialAuthenticationProvider de @microsoft/microsoft-graph-client/authProviders/azureTokenCredentials pour exécuter ce code.
Commentaires
Cette page a-t-elle été utile ?
No
Vous avez besoin d’aide pour cette rubrique ?
Vous souhaitez essayer d’utiliser Ask Learn pour clarifier ou vous guider dans cette rubrique ?