Skip to content

Instantly share code, notes, and snippets.

@pkhabazi
Created November 1, 2019 13:16
Show Gist options
  • Save pkhabazi/11d45380977ed3f23c7d208faad8e64f to your computer and use it in GitHub Desktop.
Save pkhabazi/11d45380977ed3f23c7d208faad8e64f to your computer and use it in GitHub Desktop.
$body = @{
client_id = $ClientId
scope = "https://graph.microsoft.com/.default"
client_secret = $ClientSecret
grant_type = "client_credentials"
}
$uri = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$response = Invoke-RestMethod -Method post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body
if ($response.Access_Token) {
Write-Output "Creating header for Authorization token"
$script:authToken = @{
'Content-Type' = 'application/json'
'Authorization' = "Bearer " + $response.Access_Token
'ExpiresOn' = ([timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($response.expires_on)))
'refresh_token' = $response.refresh_token
}
Write-Output "Successfully authenticated!"
}
else {
Write-Verbose $_
Write-Error "Unable to authenticate" -ErrorAction Stop
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment