Skip to content

Latest commit

 

History

History
101 lines (76 loc) · 2.76 KB

authentication_samples.md

File metadata and controls

101 lines (76 loc) · 2.76 KB

DELEGATED ACCESS SAMPLES (REQUIRES SIGNED IN USER)

1. DEVICE CODE FLOW

import asyncio

from azure.identity import DeviceCodeCredential
from msgraph import GraphServiceClient

# Create a credential object. Used to authenticate requests
credential = DeviceCodeCredential(
    client_id='CLIENT_ID',
    tenant_id='TENANT_ID',
    )

scopes = ["User.Read"]

# Create an API client with the credentials and scopes.
client = GraphServiceClient(credentials=credential, scopes=scopes)

# GET A USER USING THE USER ID (GET /users/{id})
async def get_user():
    user = await client.users_by_id('USER_ID').get()
    if user:
        print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())

2. INTERACTIVE BROWSER FLOW

import asyncio
from azure.identity import InteractiveBrowserCredential
from msgraph import GraphServiceClient

# Create a credential object. Used to authenticate requests 
credential = InteractiveBrowserCredential()
scopes = ["User.Read"]

# Create an API client with the credentials and scopes.
client = GraphServiceClient(credentials=credential, scopes=scopes)

# GET A USER USING THE USER ID (GET /users/{id})
async def get_user():
    user = await client.users_by_id('USER_ID').get()
    if user:
        print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())

APPLICATION ACCESS SAMPLES (APPLICATIONS ONLY)

3. CLIENT SECRET CREDENTIALS FLOW

import asyncio

from azure.identity import ClientSecretCredential
from msgraph import GraphServiceClient

# Create a credential object. Used to authenticate requests
credential = ClientSecretCredential(
    tenant_id='TENANT_ID',
    client_id='CLIENT_ID',
    client_secret='CLIENT_SECRET'
)
scopes = ['https://graph.microsoft.com/.default']

# Create an API client with the credentials and scopes.
client = GraphServiceClient(credentials=credential, scopes=scopes)

# GET A USER USING THE USER ID (GET /users/{id})
async def get_user():
    user = await client.users.by_user_id('USER_ID').get()
    if user:
        print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())

4. ENVIRONMENT CREDENTIAL FLOW (ASYNC)

import asyncio

from azure.identity.aio import EnvironmentCredential
from msgraph import GraphServiceClient

# Create a credential object. Used to authenticate requests
credential = EnvironmentCredential()
scopes = ['https://graph.microsoft.com/.default']

# Create an API client with the credentials and scopes.
client = GraphServiceClient(credentials=credential, scopes=scopes)

# GET A USER USING THE USER ID (GET /users/{id})
async def get_user():
    user = await client.users.by_user_id('USER_ID').get()
    if user:
        print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())