[英]PowerShell script return "The remote server returned an error: (401) Unauthorized"
[英]The remote server returned an error : (401) Unauthorized - PowerShell - Microsoft Graph API
我正在嘗試獲取 resourceProvisioningOptions = Team 的所有組的列表,這里是 url,它通過 API 調用獲取所有數據:
$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
但是,即使我擁有進行 API 調用所需的所有權限,當我嘗試運行腳本時仍會收到 401 錯誤。
您可能在 AAD 應用程序中選擇了應用程序權限。 還有一個額外的步驟。 您需要在應用程序注冊門戶中管理員同意您的應用程序才能使用 Group.Read.All 來運行它。 如果您尚未在 UI 中執行此操作,則會失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.