请求正文必须包含PowerShell中以下参数错误

问题描述 投票:0回答:0

我正在尝试获取 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 powershell-3.0 azure-powershell yammer yammer-api
© www.soinside.com 2019 - 2024. All rights reserved.