【发布时间】:2019-09-20 23:05:27
【问题描述】:
我正在尝试使用 powershell 5.1 进行 Microsoft Graph API 调用。我已经注册了应用程序。我使用 AppID 和 secret 来获取 Authorization Bearer 令牌。然后我构建我的 API 请求。安全/事件端点只返回一个 Forbidden 响应。
我的调用脚本如下所示:
# Define AppId, secret and scope, your tenant name and endpoint URL
$AppId = [myAPPID]
$AppSecret = [myAPPSecret]
$Scope = "https://graph.microsoft.com/.default"
$TenantName = "[mytenant].onmicrosoft.com"
$Url = "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token"
# Add System.Web for urlencode
Add-Type -AssemblyName System.Web
$Body = @{
client_id = $AppId
client_secret = $AppSecret
scope = $Scope
grant_type = 'client_credentials'
}
# Splat the parameters for Invoke-Restmethod for cleaner code
$PostSplat = @{
ContentType = 'application/x-www-form-urlencoded'
Method = 'POST'
# Create string by joining bodylist with '&'
Body = $Body
Uri = $Url
}
$Request = Invoke-RestMethod @PostSplat
$Header = @{
Authorization = "$($Request.token_type) $($Request.access_token)"
}
$Uri = "https://graph.microsoft.com/v1.0/security/events"
$SecurityAlertsRequest = Invoke-RestMethod -Uri $Uri -Headers $Header -Method Get -ContentType "application/json"
只是想看看我是否遗漏了一些明显的东西。是否有任何其他原因会针对此配置响应 Forbidden?
【问题讨论】:
标签: powershell azure-active-directory microsoft-graph-api