我尝试使用以下代码列出使用Office 365 Unified API的用户:
$TenantID = "xxx"
$F_ClientID = "yyy"
$F_ClientSecret = "zzz"
Add-Type @'
using System;
public class OAuthContext{
public string AccessToken{get;set;}
public string TokenType{get;set;}
public string ExpiresIn{get;set;}
public string RefreshToken{get;set;}
}
'@
$Uri = "https://login.microsoftonline.com/$($TenantID)/oauth2/token"
$ContentType = 'application/x-www-form-urlencoded'
$Headers = @{}
$Body = [System.Text.Encoding]::UTF8.GetBytes('grant_type=client_credentials&client_id='+$F_ClientID+'&client_secret='+$F_Clie ntSecret+'&resource"=https://graph.microsoft.com')
$Response = Invoke-RestMethod -Method POST -Uri $Uri -Headers $Headers -ContentType $ContentType -Body $Body
$Response
$Context = New-Object OAuthContext
$Context.AccessToken = $Response.access_token
$Context.ExpiresIn = $Response.expires_in
$Context.RefreshToken = $Response.refresh_token
$Context.TokenType = $Response.token_type
$Context
$Headers = @{}
$Headers.Add('Authorization',$Context.TokenType + ' ' + $Context.AccessToken)
$Headers
$Uri = "https://graph.microsoft.com/v1.0/users"
Invoke-RestMethod -Method GET -Uri $Uri -Headers $Headers
从结果中可以看出,访问令牌似乎已成功生成。 但是在尝试列出用户时,我收到以下错误:
Invoke-RestMethod : {
"error": {
"code": "InvalidAuthenticationToken",
"message": "CompactToken parsing failed with error code: -2147184105",
"innerError": {
"request-id": "067c7044-0c59-4a39-86ac-b89e6b13229c",
"date": "2016-02-12T17:09:56"
}
}
}
At line:41 char:1
+ Invoke-RestMethod -Method GET -Uri $Uri -Headers $Headers
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
我真的不知道我在做错了什么! 谢谢你的帮助!
答案 0 :(得分:4)
响应实际上表明访问令牌未成功生成或传递到图表端点。 Microsoft Graph无法将其解析为JWT令牌,因此尝试将其作为Microsoft Account / Live Id压缩令牌进行处理,该令牌也失败了。请检查您通过login.microsoftonline.com调用获得的响应,并且传递给graph.microsoft.com的令牌是有效的JWT令牌。
答案 1 :(得分:0)
您能否在发送此客户端密码时检查您发送的客户端密码是否与page的结果相符?
收件人在查看“应用程序/ x-www-form-urlencoded”时将解码该url,并且如果您的客户机密编码不正确,则某些字符将消失。 (这是我的问题)
答案 2 :(得分:-1)
我建议首先使用图形浏览器工具测试您要发送到图形API的查询。然后在PS脚本中模仿相同的请求。