Edit

Call Microsoft Graph as a user with the Auth SDK sidecar

Use delegated Microsoft Graph permissions when an operation should run with the signed-in user's access.

Prerequisites

Configure access

  1. Declare the Microsoft Graph delegated permission User.Read on the blueprint.
  2. Obtain user or administrator consent for User.Read on the agent identity.
  3. Configure a sidecar API, such as graph, with the Microsoft Graph base URL and delegated scopes.

Request only the scopes required by the operation.

Configure the sidecar

Add a delegated profile for Microsoft Graph:

{
  "DownstreamApis": {
    "graph": {
      "BaseUrl": "https://graph.microsoft.com/v1.0/",
      "Scopes": [
        "User.Read"
      ]
    }
  }
}

Replace User.Read with the delegated Microsoft Graph permissions required by the operation. Use the shared client ID and keep identity overrides disabled as described in Configure the Auth SDK sidecar.

Read the signed-in user's profile

This example uses Node.js 18 or later and the built-in Fetch API. Pass the access token issued to the signed-in user for your agent API to the function. User.Read is the least-privileged delegated permission for this request.

The sidecar request uses the configured delegated profile and the inbound user token:

GET /AuthorizationHeader/graph
Authorization: Bearer <agent-api-access-token>
async function getMyGraphProfile(agentApiAccessToken) {
  const tokenResponse = await fetch("http://localhost:5000/AuthorizationHeader/graph", {
    headers: { Authorization: `Bearer ${agentApiAccessToken}` }
  });
  if (!tokenResponse.ok) {
    throw new Error(`Sidecar token request failed (HTTP ${tokenResponse.status}); check the inbound user token and User.Read consent.`);
  }
  const { authorizationHeader } = await tokenResponse.json();

  const response = await fetch("https://graph.microsoft.com/v1.0/me?$select=id,displayName", {
    headers: { Authorization: authorizationHeader }
  });
  if (!response.ok) {
    throw new Error(`Microsoft Graph returned HTTP ${response.status}; verify User.Read consent and the signed-in user's access.`);
  }
  return response.json();
}

Call getMyGraphProfile(agentApiAccessToken) while handling that user's request. A successful request returns 200 OK with that user's ID and display name. If Graph returns 403, verify that User.Read is consented and the access token represents the expected signed-in user; then retry. If the sidecar returns 401, check that the inbound token is an access token for the agent API and that delegated consent is in place. After changing consent, retry with a newly issued agent API access token. Don't replace this flow with an app-only token or /AuthorizationHeaderUnauthenticated; Microsoft Graph doesn't support application permissions for /me. See the Get a user API for the delegated permission requirement.