クライアント アプリは、Microsoft Graph 関数へのタイプ セーフ アクセスを提供する graph プロパティを公開します。 グラフ関数が呼び出されると、呼び出しを認証および承認できるように、アプリは MSAL ベアラー トークンを要求にアタッチします。
Graph 関数の呼び出し
アプリ インスタンスを構築して起動した後、 app.graph クライアントを使用して任意のグラフ関数を呼び出すことができます。
import { App } from '@microsoft/teams.client';
import * as endpoints from '@microsoft/teams.graph-endpoints';
const app = new App(clientId);
await app.start();
const top10Chats = await app.graph.call(endpoints.chats.list, { $top: 10 });
最適な結果を得るには、呼び出しを試みる前に、Graph API で必要なアクセス許可にユーザーが同意していることを確認することをお勧めします。 そうしないと、呼び出しがグラフ サーバーによって拒否される可能性があります。
Graph API とアクセス許可
グラフ API が異なれば、アクセス許可の要件も異なります。 アプリ開発者は、グラフ API を呼び出す前に、同意が付与されていることを確認する必要があります。 同意の要求とテストを支援するために、クライアント アプリは次の 3 つの方法を提供します:
- アプリ起動時の予熱。
- まだ許可されていない場合は同意を要求する。
- プロンプトを表示せずに同意をテストする。
アプリ起動中の予熱処理
アプリのコンストラクターは、アプリの起動中にスコープの同意を要求する方法を制御できるオプションを受け取ります。 このオプションの詳細については、 アプリ オプション のドキュメントを参照してください。
まだ許可されていない場合は同意を要求する
アプリは、ユーザーが特定のスコープのセットに同意したかどうかをテストし、同意がまだ付与されていない場合はユーザーにプロンプトを表示する ensureConsentForScopes メソッドを提供します。
このメソッドは、ユーザーがリストされているすべてのスコープに既に同意した場合に true に解決される promise を返します。ユーザーがプロンプトを拒否した場合は false になります。
この方法は、増分の Just-In-Time 同意モデルを構築したり、同意を事前にウォームアップする方法を完全に制御したりするのに役立ちます。
import { App } from '@microsoft/teams.client';
import * as endpoints from '@microsoft/teams.graph-endpoints';
const app = new App(clientId, {
msalOptions: { prewarmScopes: ['User.Read'] },
});
// this will prompt for the User.Read scope if not already granted
await app.start();
// this will prompt for Chat.ReadBasic if not already granted
const canReadChat = await app.ensureConsentForScopes(['Chat.ReadBasic']);
if (canReadChat) {
const top10Chats = await app.graph.call(endpoints.chats.list, { $top: 10 });
// ... do something useful ...
}
プロンプトを表示せずに同意をテストする
アプリには、プロンプトを表示せずに同意をテストする hasConsentForScopes メソッドも用意されています。 これは、ユーザーの選択に基づいて機能を有効または無効にしたり、 ensureConsentForScopes でプロンプトを表示する前にわかりやすいメッセージを提供したりするのに便利です。
import { App } from '@microsoft/teams.client';
import * as endpoints from '@microsoft/teams.graph-endpoints';
const app = new App(clientId);
// this will prompt for the '.default' scope if the user hasn't already
// consented to any scope
await app.start();
// this will not raise a prompt under any circumstance
const canReadChat = await app.hasConsentForScopes(['Chat.ReadBasic']);
if (canReadChat) {
const top10Chats = await app.graph.call(endpoints.chats.list, { $top: 10 });
// ... do something useful ...
}