Microsoft Graph JavaScript クライアント ライブラリは、Microsoft Graph API の軽量ラッパーであり、サーバー側およびブラウザー内で使用できます。
モデル (ユーザー、グループなど) で IntelliSense を探していますか ?Microsoft Graph Types リポジトリをご覧ください !
npm install @microsoft/microsoft-graph-clientモジュールに @microsoft/microsoft-graph-client をインポートします。また、isomorphic-fetch のようなフェッチのためのポリフィルが必要になります。
import "isomorphic-fetch";
import { Client } from "@microsoft/microsoft-graph-client";graph-js-sdk.js を HTML ページに含めます。
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@microsoft/microsoft-graph-client/lib/graph-js-sdk.js"></script>ブラウザが Fetch [サポート] または Promise [サポート] をサポートしていない場合、フェッチには github/fetch、Promise には es6-promise のようなポリフィルを使用する必要があります。
<!-- polyfilling promise -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/es6-promise/dist/es6-promise.auto.min.js"></script>
<!-- polyfilling fetch -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/whatwg-fetch/dist/fetch.umd.min.js"></script>
<!-- depending on your browser you might wanna include babel polyfill -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@babel/[email protected]/dist/polyfill.min.js"></script>サポートされている次の認証ポータルのいずれかを使用して、Microsoft Graph API を使用するアプリケーションを登録します。
- Microsoft アプリケーション登録ポータル:統合 V2 認証エンドポイントを使用して、Microsoft アカウントや組織のアカウントで機能する新しいアプリケーションを登録します。
- Microsoft Azure Active Directoryテナントまたは複数のテナントで職場または学校のユーザーをサポートするために、テナントの Active Directory に新しいアプリケーションを登録します。
Microsoft Graph JavaScript クライアント ライブラリには、accessToken の取得を処理する MSAL (Microsoft Authentication Library) のアダプター実装 (ImplicitMSALAuthenticationProvider) があります。MSAL ライブラリはこのライブラリに同梱されていません。ユーザーは外部でそれを含める必要があります (MSAL を含めるには、こちらを参照してください)。
**重要なメモ:**MSAL は、フロントエンド アプリケーションでのみサポートされます。サーバー側の認証では、独自に AuthenticationProvider を実装する必要があります。カスタム認証プロバイダーを作成する方法について説明します。
互換性のある MSAL バージョンについては、package.json の devDependencies を参照し、以下でそのバージョンを更新してください。
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/<version>/js/msal.min.js"></script>// Configuration options for MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/MSAL.js-1.0.0-api-release#configuration-options
const msalConfig = {
auth: {
clientId: "your_client_id", // Client Id of the registered application
redirectUri: "your_redirect_uri",
},
};
const graphScopes = ["user.read", "mail.send"]; // An array of graph scopes
// Important Note: This library implements loginPopup and acquireTokenPopup flow, remember this while initializing the msal
// Initialize the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js#1-instantiate-the-useragentapplication
const msalApplication = new Msal.UserAgentApplication(msalConfig);
const options = new MicrosoftGraph.MSALAuthenticationProviderOptions(graphScopes);
const authProvider = new MicrosoftGraph.ImplicitMSALAuthenticationProvider(msalApplication, options);互換性のある MSAL バージョンについては、package.json の devDependencies を参照し、以下でそのバージョンを更新してください。
npm install msal@<version>import { UserAgentApplication } from "msal";
import { ImplicitMSALAuthenticationProvider } from "./node_modules/@microsoft/microsoft-graph-client/lib/src/ImplicitMSALAuthenticationProvider";
// An Optional options for initializing the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/MSAL-basics#configuration-options
const msalConfig = {
auth: {
clientId: "your_client_id", // Client Id of the registered application
redirectUri: "your_redirect_uri",
},
};
const graphScopes = ["user.read", "mail.send"]; // An array of graph scopes
// Important Note: This library implements loginPopup and acquireTokenPopup flow, remember this while initializing the msal
// Initialize the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js#1-instantiate-the-useragentapplication
const msalApplication = new UserAgentApplication(msalConfig);
const options = new MicrosoftGraph.MSALAuthenticationProviderOptions(graphScopes);
const authProvider = new ImplicitMSALAuthenticationProvider(msalApplication, options);ユーザーは IAuthenticationProvider インターフェイスを実装することにより、自分好みの認証ライブラリを統合できます。カスタム認証プロバイダーの実装を参照してください。
Client クラスのインスタンスは、Microsoft Graph API への要求に対処し、応答を処理します。このクラスの新しいインスタンスを作成するには、ClientOptions の authProvider キーの値として静的初期化メソッド Client.initWithMiddleware に渡す必要がある IAuthenticationProvider のインスタンスを提供する必要があります。
const options = {
authProvider, // An instance created from previous step
};
const Client = MicrosoftGraph.Client;
const client = Client.initWithMiddleware(options);import { Client } from "@microsoft/microsoft-graph-client";
const options = {
authProvider, // An instance created from previous step
};
const client = Client.initWithMiddleware(options);クライアントの初期化の詳細については、こちらのドキュメントを参照してください。
認証のセットアップとクライアントのインスタンスを用意したら、サービスの呼び出しを開始できます。すべてのリクエストは client.api(path) で始まり、action で終わる必要があります。
ユーザーの詳細の取得
try {
let userDetails = await client.api("/me").get();
console.log(userDetails);
} catch (error) {
throw error;
}受信者にメールを送信する
// Construct email object
const mail = {
subject: "Microsoft Graph JavaScript Sample",
toRecipients: [
{
emailAddress: {
address: "[email protected]",
},
},
],
body: {
content: "<h1>MicrosoftGraph JavaScript Sample</h1>Check out https://github.com/microsoftgraph/msgraph-sdk-javascript",
contentType: "html",
},
};
try {
let response = await client.api("/me/sendMail").post({ message: mail });
console.log(response);
} catch (error) {
throw error;
}詳細については、以下を参照してください:呼び出しパターン、アクション、クエリ パラメーター、API メソッドおよびその他。
Microsoft Graph JavaScript クライアント ライブラリに関するフィードバックをお寄せください。質問や提案につきましては、このリポジトリの「問題」セクションで送信できます。
投稿ガイドラインを参照してください。
- Microsoft Graph Web サイト
- Microsoft Graph TypeScript 型
- Microsoft Graph を使った Angular の単一ページ アプリの作成
- Microsoft Graph を使った Node.js Express アプリの作成
- Office デベロッパー センター
package.json に含まれるパッケージの詳細については、サードパーティについての通知を参照してください
ライブラリまたはサービスでセキュリティに関する問題を発見した場合は、できるだけ詳細に [email protected] に報告してください。提出物は、Microsoft Bounty プログラムを通じて報酬を受ける対象となる場合があります。セキュリティの問題を GitHub の問題や他のパブリック サイトに投稿しないでください。情報を受け取り次第、ご連絡させていただきます。セキュリティの問題が発生したときに通知を受け取ることをお勧めします。そのためには、このページにアクセスし、セキュリティ アドバイザリ通知を受信登録してください。
Copyright (c) Microsoft Corporation。All rights reserved.MIT ライセンス ("ライセンス") に基づいてライセンスされています。
このプロジェクトでは、Microsoft Open Source Code of Conduct (Microsoft オープン ソース倫理規定) が採用されています。詳細については、「Code of Conduct FAQ (倫理規定の FAQ)」を参照してください。また、その他の質問やコメントがあれば、[email protected] までお問い合わせください。