【发布时间】:2018-10-05 16:36:35
【问题描述】:
我正在尝试检索审核日志以跟踪用户许可证分配更改。 这是我在 MS Graph Explorer 中使用的 URI。它在那里工作得很好,我可以从中获取我想要的数据。
https://graph.microsoft.com/beta/auditLogs/directoryAudits?$filter=activityDisplayName eq 'Update user'
但是,当我尝试使用使用完全相同的 Office 365 凭据生成的身份验证令牌通过 PowerShell 使用相同的 URI 进行查询时,我收到错误消息 Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
在获取身份验证令牌方面我是否遗漏了什么? 这是我使用的 GetAuthToken 函数
Function GetAuthToken
{
param
(
[Parameter(Mandatory=$true)]
$TenantName
)
Import-Module Azure
$clientId = "ef9bcdf0-a675-4cd5-9ec3-fa549f9ee4cf"
$redirectUri = "https://RedirectURI.com"
$resourceAppIdURI = "https://graph.microsoft.com"
$authority = "https://login.microsoftonline.com/$TenantName"
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
$Credential = Import-Clixml -Path "C:\MIMA\tom_admin_cred.xml"
$AADCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList $credential.UserName,$credential.Password
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId,$AADCredential)
return $authResult
}
这是我用来检索报告的代码
$token = GetAuthToken -TenantName $tenant
$authHeader = @{
'Content-Type'='application\json'
'Authorization'=$token.CreateAuthorizationHeader()
}
$uri = "https://graph.microsoft.com/beta/auditLogs/directoryAudits?$filter=activityDisplayName eq 'Update user'"
$auditReports = Invoke-RestMethod -Uri $uri –Headers $authHeader –Method Get
【问题讨论】:
标签: powershell microsoft-graph-api office365api