Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save poiriersimon/e0ce1c02dff7db6fd8472e501ce40b7c to your computer and use it in GitHub Desktop.
Save poiriersimon/e0ce1c02dff7db6fd8472e501ce40b7c to your computer and use it in GitHub Desktop.
Microsoft Graph API Powershell Example - App with Secret
#Require an AzureAD App with Microsoft Graph API and
# App Permission
# - Read mail in all mailboxes
# Impersonation Permission
# - Read user and shared mail
# Don't forget to grant permission
#Pre-reqs for REST API calls
$ClientID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
$ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX="
$user = "user@TENANTNAME.onmicrosoft.com"
$tenantdomain = "TENANTNAME.onmicrosoft.com"
#API info
$loginURL = "https://login.microsoftonline.com/"
$resource = "https://graph.microsoft.com"
# Get an Oauth 2 access token based on client id, secret and tenant domain
$body = @{grant_type="client_credentials";resource=$resource;client_id=$ClientID;client_secret=$ClientSecret}
$oauth = Invoke-RestMethod -Method Post -Uri $loginURL/$tenantdomain/oauth2/token?api-version=1.0 -Body $body
#Let's put the oauth token in the header, where it belongs
$headerParams = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"}
#GET
$uri ="https://graph.microsoft.com/v1.0/users/$($user)/Messages"
$tenantInfo = (Invoke-RestMethod -Uri $uri –Headers $headerParams –Method Get –Verbose).value
$tenantInfo | select Subject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment