Created
September 21, 2018 17:39
-
-
Save poiriersimon/e0ce1c02dff7db6fd8472e501ce40b7c to your computer and use it in GitHub Desktop.
Microsoft Graph API Powershell Example - App with Secret
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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