Der Microsoft Graph-Client dient dazu, Aufrufe von Microsoft Graph einfacher zu gestalten. Sie können eine einzelne Client-Instanz für die gesamte Lebensdauer der Anwendung verwenden. Informationen zum Hinzufügen und Installieren des Microsoft Graph-Clientpakets zu Ihrem Projekt finden Sie unter Installieren des SDK.
Die folgenden Codebeispiele zeigen, wie Sie eine Instanz eines Microsoft Graph-Clients mit einem Authentifizierungsanbieter in den unterstützten Sprachen erstellen. Der Authentifizierungsanbieter übernimmt das Abrufen von Zugriffstoken für die Anwendung.
Für jede Sprache und Plattform sind viele verschiedene Authentifizierungsanbieter verfügbar. Die verschiedenen Authentifizierungsanbieter unterstützen unterschiedliche Clientszenarien. Weitere Informationen darüber, welcher Anbieter und welche Optionen für Ihr Szenario angemessen sind, finden Sie unter Auswählen eines Authentifizierungsanbieters.
Die Client-ID ist die App-Registrierungs-ID, die generiert wird, wenn Sie Ihre App im Azure-Portal registrieren.
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);
Schließen Sie using
Anweisungen für Azure.Identity
und Microsoft.Graph
ein, um diesen Code auszuführen.
cred, _ := azidentity.NewDeviceCodeCredential(&azidentity.DeviceCodeCredentialOptions{
TenantID: "TENANT_ID",
ClientID: "CLIENT_ID",
UserPrompt: func(ctx context.Context, message azidentity.DeviceCodeMessage) error {
fmt.Println(message.Message)
return nil
},
})
graphClient, _ := graph.NewGraphServiceClientWithCredentials(
cred, []string{"User.Read"})
Fügen Sie "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
und graph "github.com/microsoftgraph/msgraph-sdk-go"
in Ihren Importblock ein, um diesen Code auszuführen.
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);
Schließen Sie import
Anweisungen für com.azure.identity.DeviceCodeCredential
, com.azure.identity.DeviceCodeCredentialBuilder
und com.microsoft.graph.serviceclient.GraphServiceClient
ein, um diesen Code auszuführen.
$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);
Schließen Sie use
Anweisungen für Microsoft\Graph\GraphRequestAdapter
, Microsoft\Graph\GraphServiceClient
und Microsoft\Kiota\Abstractions\Authentication\BaseBearerTokenAuthenticationProvider
ein, um diesen Code auszuführen.
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)
Schließen Sie import
Anweisungen für DeviceCodeCredential
und GraphServiceClient
von msgraph.graph_service_client
azure.identity
ein, um diesen Code auszuführen.
// @azure/identity
const credential = new DeviceCodeCredential({
tenantId: 'YOUR_TENANT_ID',
clientId: 'YOUR_CLIENT_ID',
userPromptCallback: (info) => {
console.log(info.message);
},
});
// @microsoft/microsoft-graph-client/authProviders/azureTokenCredentials
const authProvider = new TokenCredentialAuthenticationProvider(credential, {
scopes: ['User.Read'],
});
const graphClient = Client.initWithMiddleware({ authProvider: authProvider });
Schließen Sie import
Anweisungen für DeviceCodeCredential
aus @azure/identity
, Client
und @microsoft/microsoft-graph-client
TokenCredentialAuthenticationProvider
von ein@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials
, um diesen Code auszuführen.