我尝试获取 AAD 令牌,然后使用 Yammer API 获取 Yammer 消息,但收到此错误:
Invoke-RestMethod : {"error":"invalid_request","error_description":"AADSTS900144: The request body must contain the following parameter: 'code'.\r\nTrace ID: b4f13dec-5b00-446d-b6b0-9b03e1de2700\r\nCorrelation ID:
61ff7b84-22eb-4176-a7e9-eec721c73d60\r\nTimestamp: 2023-07-26 14:59:00Z","error_codes":[900144],"timestamp":"2023-07-26
14:59:00Z","trace_id":"b4f13dec-5b00-446d-b6b0-9b03e1de2700","correlation_id":"61ff7b84-22eb-4176-a7e9-eec721c73d60","error_uri":"https://login.microsoftonline.com/error?code=900144"}
At line:18 char:15
我在正文中添加了“代码”,但是我怎样才能获得
$authorizationCode
?
$ClientId = ""
$SecretID = ""
$tenantid = ""
$params = @{
Uri = "https://login.microsoftonline.com/$($tenantid)/oauth2/v2.0/token"
Method = "POST"
Body = @{
"client_id" = $ClientId
"client_secret" = $SecretID
"grant_type" = 'authorization_code'
"code" = $authorizationCode
"scope" = "https://api.yammer.com/user_impersonation"
"username" = "";
"password" = "";
}
}
$connection = Invoke-RestMethod @params
$headers = @{ Authorization=("Bearer " + $connection.access_token) }
$webRequest = Invoke-WebRequest –Uri "https://www.yammer.com/api/v1/messages.json" –Method Get -Headers $headers
if ($webRequest.StatusCode -eq 200) {
$results = $webRequest.Content | ConvertFrom-Json
$results.messages | ForEach-Object {
$message = $_
Write-Host $message.sender_id $message.body
}
}
else {
Write-Host "An error has occurred: " + $webRequest.StatusCode
}
发生错误是因为您没有在 PowerShell 脚本中包含使用授权代码流获取令牌所需的
value。$authorizationCode
我注册了一个 Azure AD 应用程序并授予了 Yammer API 权限:
当我在我的环境中运行您的 PowerShell 脚本时,我遇到了相同的错误,如下所示:
要解决错误,您需要通过在需要redirect_uri
的浏览器中运行授权请求来获取代码
值。
我在 Azure AD 应用程序的
Authentication选项卡中将
redirect_uri
添加为 https://jwt.ms:
当我在浏览器中运行以下授权请求时,登录后我在地址栏中获得了code值:
https://login.microsoftonline.com/tenantID/oauth2/v2.0/authorize?
client_id=appID
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope=https://api.yammer.com/user_impersonation
&state=12345
回复:
现在,我通过添加 code 值运行下面修改后的脚本并成功获得响应:
$ClientId = "appID"
$Secret = "secret"
$tenantid = "tenantID"
#Make sure to include this variable with code value
$authorizationCode = "paste_code_from_above_request"
$params = @{
Uri = "https://login.microsoftonline.com/$($tenantid)/oauth2/v2.0/token"
Method = "POST"
Body = @{
"client_id" = $ClientId
"client_secret" = $Secret
"grant_type" = 'authorization_code'
"code" = $authorizationCode
"scope" = "https://api.yammer.com/user_impersonation"
"redirect_uri" = "https://jwt.ms"
}
}
$connection = Invoke-RestMethod @params
$headers = @{ Authorization=("Bearer " + $connection.access_token) }
$webRequest = Invoke-WebRequest –Uri "https://www.yammer.com/api/v1/messages.json" –Method Get -Headers $headers
if ($webRequest.StatusCode -eq 200) {
$results = $webRequest.Content | ConvertFrom-Json
$results.messages | ForEach-Object {
$message = $_
Write-Host $message.sender_id $message.body
}
}
else {
Write-Host "An error has occurred: " + $webRequest.StatusCode
}
回复: