Skip to content

Instantly share code, notes, and snippets.

@junecastillote
Last active July 8, 2019 02:32
Show Gist options
  • Save junecastillote/5b478a9aa3bf4b4efdae4f5da2ced429 to your computer and use it in GitHub Desktop.
Save junecastillote/5b478a9aa3bf4b4efdae4f5da2ced429 to your computer and use it in GitHub Desktop.
PowerShell Function to get Microsoft Graph API token using your app's client ID, client Secret and Tenant Domain
Function New-MSGraphAPIToken {
<#
.SYNOPSIS
Acquire authentication token for MS Graph API
.DESCRIPTION
If you have a registered app in Azure AD, this function can help you get the authentication token
from the MS Graph API endpoint. Each token is valid for 60 minutes.
.PARAMETER appID
This is the registered appID in AzureAD
.PARAMETER appKey
This is the key of the registered app in AzureAD
.PARAMETER domain
This is your Office 365 Tenant Domain
.EXAMPLE
$graphToken = New-MSGraphAPIToken -appID <appID> -appKey <appKey> -domain <tenant domain>
The above example gets a new token using the appID, appKey and tenant domain combination
.NOTES
General notes
#>
param(
[parameter(mandatory=$true)]
[string]$appID,
[parameter(mandatory=$true)]
[string]$appKey,
[parameter(mandatory=$true)]
[string]$domain
)
$body = @{grant_type="client_credentials";scope="https://graph.microsoft.com/.default";client_id=$appID;client_secret=$appKey}
$oauth = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$domain/oauth2/v2.0/token -Body $body
$token = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"}
Return $token
}
Function New-OutlookRestAPIToken {
<#
.SYNOPSIS
Acquire authentication token for Outlook REST API
.DESCRIPTION
If you have a registered app in Azure AD, this function can help you get the authentication token
from the Outlook REST API endpoint. Each token is valid for 60 minutes.
.PARAMETER appID
This is the registered appID in AzureAD
.PARAMETER appKey
This is the key of the registered app in AzureAD
.PARAMETER domain
This is your Office 365 Tenant Domain
.EXAMPLE
$graphToken = New-OutlookRestAPIToken -appID <appID> -appKey <appKey> -domain <tenant domain>
The above example gets a new token using the appID, appKey and tenant domain combination
.NOTES
General notes
#>
param(
[parameter(mandatory=$true)]
[string]$appID,
[parameter(mandatory=$true)]
[string]$appKey,
[parameter(mandatory=$true)]
[string]$domain
)
$body = @{grant_type="client_credentials";scope="https://outlook.office.com/.default";client_id=$appID;client_secret=$appKey}
$oauth = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$domain/oauth2/v2.0/token -Body $body
$token = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"}
Return $token
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment