You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DELEGATED ACCESS SAMPLES (REQUIRES SIGNED IN USER)
1. DEVICE CODE FLOW
importasynciofromazure.identityimportDeviceCodeCredentialfrommsgraph_betaimportGraphServiceClient# Set the event loop policy for Windowsasyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Create authentication provider object. Used to authenticate requestscredential=DeviceCodeCredential(
client_id='CLIENT_ID',
tenant_id='TENANT_ID',
)
scopes= ["User.Read"]
# Create an API client with the credentials and scopes.client=GraphServiceClient(credential, scopes=scopes)
# GET A USER USING THE USER ID (GET /users/{id})asyncdefget_user():
user=awaitclient.users_by_id('USER_ID').get()
ifuser:
print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())
2. INTERACTIVE BROWSER FLOW
importasynciofromazure.identityimportInteractiveBrowserCredentialfrommsgraph_betaimportGraphServiceClient# Set the event loop policy for Windowsasyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Create authentication provider object. Used to authenticate requestscredential=InteractiveBrowserCredential()
scopes= ["User.Read"]
# Create an API client with the credentials and scopes.client=GraphServiceClient(credential, scopes=scopes)
# GET A USER USING THE USER ID (GET /users/{id})asyncdefget_user():
user=awaitclient.users_by_id('USER_ID').get()
ifuser:
print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())
APPLICATION ACCESS SAMPLES (APPLICATIONS ONLY)
3. CLIENT SECRET CREDENTIALS FLOW
importasynciofromazure.identityimportClientSecretCredentialfrommsgraph_betaimportGraphServiceClient# Set the event loop policy for Windowsasyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Create authentication provider object. Used to authenticate requestcredential=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(credential, scopes=scopes)
# GET A USER USING THE USER ID (GET /users/{id})asyncdefget_user():
user=awaitclient.users.by_user_id('USER_ID').get()
ifuser:
print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())
4. ENVIRONMENT CREDENTIAL FLOW (ASYNC)
importasynciofromazure.identity.aioimportEnvironmentCredentialfrommsgraph_betaimportGraphServiceClient# Set the event loop policy for Windowsasyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Create authentication provider object. Used to authenticate requestcredential=EnvironmentCredential()
scopes= ['https://graph.microsoft.com/.default']
# Create an API client with the credentials and scopes.client=GraphServiceClient(credential, scopes=scopes)
# GET A USER USING THE USER ID (GET /users/{id})asyncdefget_user():
user=awaitclient.users.by_user_id('USER_ID').get()
ifuser:
print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())