The remote server returned an error : (401) Unauthorized - PowerShell - Microsoft Graph API

by Arif Khan   Last Updated October 11, 2019 22:26 PM - source

I am trying to get the list of all groups where the resourceProvisioningOptions = Team, here is the url which gets all the data through API call:

$clientID = xxxx
$tenantName = xxxx
$ClientSecret = xxxx
$resource = "https://graph.microsoft.com/"


$ReqTokenBody = @{
Grant_Type    = "client_credentials"
Scope         = "https://graph.microsoft.com/.default"
client_Id     = $clientID
Client_Secret = $clientSecret 
} 

$authheader = @{

    'Authorization' = "Bearer $($Tokenresponse.access_token)"
    'Content-Type'='application\json'

}

$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody

$test = "'Team'"

$apiUrl = 'https://graph.microsoft.com/beta/groups?$filter=resourceProvisioningOptions/Any(x:x eq {0})' -f $test

$Data = Invoke-RestMethod -Uri $apiUrl -Headers $authheader -Body $ReqTokenBody -Method Get


$Groups = ($Data | select-object Value).Value | Select-Object  displayName, id, description, mail | Out-File .\texxtfile.txt

However, I am getting a 401 error when I try to run the script even though I have all the permissions required to make the API call.



Related Questions




Microsoft team converted from unified group

Updated June 30, 2019 08:26 AM