This demo application showcases how to use Microsoft Graph APIs within a Teams bot, including both delegated (user) and app-only access patterns.
- User Authentication: Teams OAuth integration with automatic token management
- Profile Information: Retrieve and display user profile data via delegated access
- Email Access: List recent emails with Mail.Read scope
- App-Level Graph Access: Query organization data using app-only permissions (no user sign-in needed) via
app.get_app_graph()orctx.app_graph
signin- Authenticate with Microsoft Graphprofile- Display user profile information (requires User.Read)emails- Show recent emails (requires Mail.Read permission)app-users- List organization users viaapp.get_app_graph()(app-only, no sign-in needed)app-users ctx- List organization users viactx.app_graph(app-only, no sign-in needed)signout- Sign out of Microsoft Graphhelp- Show available commands and implementation details
- Configure OAuth connection in Azure Bot registration
- Set connection name to "graph" (or update
CONNECTION_NAMEenv var) - Configure appropriate Microsoft Graph permissions:
User.Read(for profile access)Mail.Read(for email access)User.Read.Allapplication permission (for app-users commands)
- Create a
.envfile inexamples/graph/src/with required environment variables (copy fromsample.env):CLIENT_ID=<your-azure-bot-app-id> CLIENT_SECRET=<your-azure-bot-app-secret> TENANT_ID=<your-tenant-id> CONNECTION_NAME=graph # PORT=3978 # Optional: specify custom port (defaults to 3978)
NOTE: This example uses West Europe, but follow the equivalent for other locations.
- In
azurebot.bicep, replace allglobaloccurrences towesteurope - In
manifest.json, invalidDomains,*.botframework.comshould be replaced byeurope.token.botframework.com - In
aad.manifest.json, replacehttps://token.botframework.com/.auth/web/redirectwithhttps://europe.token.botframework.com/.auth/web/redirect - In
main.py, updateAppOptionsto includeapi_client_settings
app = App(
default_connection_name='graph',
api_client_settings=ApiClientSettings(
oauth_url="https://europe.token.botframework.com"
)
)From the examples/graph/ directory (so .env is discovered automatically):
cd examples/graph
uv run src/main.pyfrom microsoft_teams.graph import get_graph_client
# Delegated access — create Graph client using the user's token
graph = get_graph_client(ctx.user_token)
me = await graph.me.get()
messages = await graph.me.messages.get()
# App-only access — no user sign-in needed
graph = app.get_app_graph()
users = await graph.users.get()
# Or via context
users = await ctx.app_graph.users.get()