gpt4 book ai didi

powershell - 'CompactToken 解析失败,错误代码为 : -2147184105' when trying to access Users with Office 365 Unified API using PowerShell

转载 作者:行者123 更新时间:2023-12-04 02:26:10 25 4
gpt4 key购买 nike

我正在尝试使用以下代码列出使用 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

从结果可以看出,访问 token 似乎已成功生成。
但是在尝试列出用户时,出现以下错误:
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

我真的不知道我在这里做错了什么!
谢谢你的帮助!

最佳答案

响应实际上表明访问 token 未成功生成或传递到图形端点。 Microsoft Graph 无法将其解析为 JWT token ,因此尝试将其作为 Microsoft 帐户/Live Id 紧凑 token 进行处理,但也失败了。请检查您从对 login.microsoftonline.com 的调用获得的响应,以及传递给 graph.microsoft.com 的 token 是否是有效的 JWT token 。

关于powershell - 'CompactToken 解析失败,错误代码为 : -2147184105' when trying to access Users with Office 365 Unified API using PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35368896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com