【问题标题】:Microsoft Graph API - app in powershell - forbidden responseMicrosoft Graph API - PowerShell 中的应用程序 - 禁止响应
【发布时间】:2019-09-20 23:05:27
【问题描述】:

我正在尝试使用 powershell 5.1 进行 Microsoft Graph API 调用。我已经注册了应用程序。我使用 AppID 和 secret 来获取 Authorization Bearer 令牌。然后我构建我的 API 请求。安全/事件端点只返回一个 Forbidden 响应。

API Permissions Granted

我的调用脚本如下所示:

# 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


    【解决方案1】:

    您已向应用授予委派权限。 这些仅适用于有用户登录到您的应用并且您获得访问令牌以代表他们行事的情况。

    您需要在 Azure AD 中向您的应用授予应用程序权限

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2020-04-24
      • 1970-01-01
      • 2020-05-20
      相关资源
      最近更新 更多