hello,
I am trying to access Microsoft Graph in powershell for the first time and therefore I am using examples found on the internet.
Unfortunately when I try to run my first script, I get my connection token, but I get an error message: The remote server returned an error: (401) Unauthorized.
When declaring my application on Azure, however, I authorized the rights to the user.read.all libraries in delegated and in application ....
Do you have any idea where my problem came from?
Here is my code (app id and secret are masked for the sample):
$tenant = Read-Host("Enter Tenant Name :")
$openid = Invoke-RestMethod -uri "https://login.microsoftonline.com/$tenant/.well-known/openid-configuration"
write-host "The token endpoint of your directory is :"
$openid.token_endpoint
$token = $openid.token_endpoint
$body = @{
client_id = *my App ID is Here*
client_secret = *my App Secret Is Here*
redirect_uri = "https://localhost"
grant_type = "client_credentials"
ressource ="https://graph.microsoft.com"
tenant = $tenant
}
Write-Host "requesting access token"
$request = Invoke-RestMethod -uri $token -Body $body -Method Post
$request.access_token
$graph = "https://graph.microsoft.com/v1.0/users"
$api = Invoke-RestMethod -Headers @{Authorization = "Bearer $($request.access_token)"} -Uri $graph -Method Get
$api.value