Skip to content

Instantly share code, notes, and snippets.

@astaykov
Created October 29, 2015 20:56
Show Gist options
  • Save astaykov/756d879415a3af911c78 to your computer and use it in GitHub Desktop.
Save astaykov/756d879415a3af911c78 to your computer and use it in GitHub Desktop.
Get access token from Azure AD OAuth 2.0 endpoint
Function GetToken
{
param(
[String] $authority = "https://login.microsoftonline.com/lab09.onmicrosoft.com/oauth2/token",
[String] $clientId = "<your_client_id_here>",
[String] $clientSecret = "<client_secret>",
[String] $resourceId = "https%3a%2f%2fgraph.windows.net"
)
$body = "grant_type=client_credentials&resource=$($resourceId)&client_id=$($clientId)&client_secret=$($clientSecret)"
$res = Invoke-WebRequest -Uri $authority -Body $body -Method Post
$authResult = $res.Content | ConvertFrom-Json
return $authResult.access_token
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment