Microsoft Graph クライアントは、Microsoft Graph への呼び出しを簡単にするように設計されています。 アプリケーションの存続期間中、単一のクライアント インスタンスを使用できます。 Microsoft Graph クライアント パッケージをプロジェクトに追加してインストールする方法については、「 SDK のインストール」を参照してください。
次のコード例は、サポートされている言語で認証プロバイダーを使用して Microsoft Graph クライアントのインスタンスを作成する方法を示しています。 認証プロバイダーは、アプリケーションのアクセス トークンの取得を処理します。
言語とプラットフォームごとに、さまざまな認証プロバイダーを利用できます。 さまざまな認証プロバイダーは、さまざまなクライアント シナリオをサポートします。 シナリオに適したプロバイダーとオプションの詳細については、「認証プロバイダーの選択」を参照してください。
クライアント ID は、Azure portalでアプリを登録するときに生成されるアプリ登録 ID です。
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);
このコードを実行するには、Azure.Identity
とMicrosoft.Graph
のusing
ステートメントを含めます。
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"})
インポート ブロックに "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
と graph "github.com/microsoftgraph/msgraph-sdk-go"
を含め、このコードを実行します。
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);
このコードを実行するには、com.azure.identity.DeviceCodeCredential
、com.azure.identity.DeviceCodeCredentialBuilder
、com.microsoft.graph.serviceclient.GraphServiceClient
のimport
ステートメントを含めます。
$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);
このコードを実行するには、Microsoft\Graph\GraphRequestAdapter
、Microsoft\Graph\GraphServiceClient
、Microsoft\Kiota\Abstractions\Authentication\BaseBearerTokenAuthenticationProvider
のuse
ステートメントを含めます。
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)
このコードを実行するには、azure.identity
からDeviceCodeCredential
import
ステートメントを含め、msgraph.graph_service_client
からGraphServiceClient
します。
// @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 });
このコードを実行するには、@azure/identity
からのDeviceCodeCredential
、@microsoft/microsoft-graph-client
からのClient
、@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials
からのTokenCredentialAuthenticationProvider
のimport
ステートメントを含めます。