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
}
I registered one Azure AD application and granted consent to Yammer API permission:
When I ran your PowerShell script in my environment, I got same error as below:
To resolve the error, you need to get code value by running authorization request in browser for which
redirect_uriis needed.I added
redirect_urias https://jwt.ms in Authentication tab of Azure AD application:When I ran below authorization request in browser, I got code value in address bar after signing in:
Response:
Now, I ran below modified script by adding code value and got response successfully:
Response: