"The request body must contain the following parameter" error

536 Views Asked by At

I'm trying to get AAD token then get Yammer message using Yammer API but I'm getting this error:

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

I added "code" inside the body but how can I get the $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
}
1

There are 1 best solutions below

3
Sridevi On

The error occurred as you are not including $authorizationCode value in your PowerShell script that is needed to get token using authorization code flow.

I registered one Azure AD application and granted consent to Yammer API permission:

enter image description here

When I ran your PowerShell script in my environment, I got same error as below:

enter image description here

To resolve the error, you need to get code value by running authorization request in browser for which redirect_uri is needed.

I added redirect_uri as https://jwt.ms in Authentication tab of Azure AD application:

enter image description here

When I ran below authorization request in browser, I got code value in address bar after signing in:

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

Response:

enter image description here

Now, I ran below modified script by adding code value and got response successfully:

$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
}

Response:

enter image description here