Skip to content

Instantly share code, notes, and snippets.

@isvargasmsft
Created October 12, 2023 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isvargasmsft/8a68ab21eea974e11330d6f30d8fafec to your computer and use it in GitHub Desktop.
Save isvargasmsft/8a68ab21eea974e11330d6f30d8fafec to your computer and use it in GitHub Desktop.
Python simple initialization
import asyncio
from azure.identity.aio import ClientSecretCredential
from msgraph import GraphServiceClient
credential = ClientSecretCredential(
'tenant_id',
'client_id',
'client_secret'
)
scopes = ['https://graph.microsoft.com/.default']
client = GraphServiceClient(credentials=credential, scopes=scopes)
# GET /users/{id | userPrincipalName}
async def get_user():
user = await client.users.by_user_id('userPrincipalName').get()
if user:
print(user.display_name)
asyncio.run(get_user())
# Example using sync credentials and delegated access.
from azure.identity import DeviceCodeCredential
from msgraph import GraphServiceClient
credential=DeviceCodeCredential(
'CLIENT_ID',
'TENANT_ID',
)
scopes = ['User.Read', 'Mail.Read']
client = GraphServiceClient(credentials=credential, scopes=scopes)
# GET /me
async def me():
me = await client.me.get()
if me:
print(me.display_name)
asyncio.run(me())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment