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.identityimportDeviceCodeCredentialfrommsgraphimportGraphServiceClient# Create a credential 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(credentials=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.identityimportInteractiveBrowserCredentialfrommsgraphimportGraphServiceClient# 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})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.identityimportClientSecretCredentialfrommsgraphimportGraphServiceClient# Create a credential object. Used to authenticate requestscredential=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})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.aioimportEnvironmentCredentialfrommsgraphimportGraphServiceClient# Create a credential object. Used to authenticate requestscredential=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})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())