Hello team,
I need help on to add single hard coded users into Bulk Governance Groups from the CSV file.
I have tried to add but getting 400 bad request error. below is the sample reference code. Please help me to fix
$tenant = “dev”
$ClientID = “xx”
$clientSecret = “xx”
$userId = “0e893e829d7048cf9ab3c774c68907de”
$groupCsvFile = “C:\Users\TALEVANEN\Desktop\test.csv”
$identityType=“IDENTITY”
$identityName=“Niket Talevane”
$pair = “$($ClientID):$($clientSecret)”
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($pair))
$BasicAuth1 = “Basic $encodedCreds”
Write-Host “$BasicAuth1” $BasicAuth1
Base API URLs
$baseUrl = “https://$tenant.api.identitynow.com”
$tokenURL = “https://$tenant.api.identitynow.com/oauth/token”
Get token
$tokenParam = @{
URI = $tokenURL
Body = "grant_type=client_credentials"
Headers = @{
    'Authorization' = "$BasicAuth1"
    'Content-Type' = 'application/x-www-form-urlencoded;application/json;charset=UTF-8'
}
Method = 'POST'
}
$tokenResponse = Invoke-RestMethod @tokenParam
$token = $tokenResponse.access_token
Write-Host “Token is: $token”
Headers for API requests
$headers = @{
"Authorization" = "Bearer $token"
"Content-Type" = "application/json"
}
# Read Governance Group IDs from the file
$govGroups = Import-Csv -Path $groupCsvFile
Write-Host "Loaded Governance Groups..."
foreach ($group in $govGroups) {
$ggId = $group.GroupID 
$url = "$baseUrl/beta/workgroups/$ggId/members/bulk-add"
$body = @(@{ 
    'type'= "IDENTITY"
    'id' =  $userId
    'name'= $identityName       
    })   
  
$jsonBody = $body | ConvertTo-Json -Depth 10
Write-Host "JSON Payload: $jsonBody"
$jsonBody | Out-File "C:\Users\TALEVANEN\Desktop\payload.json"
 Write-Host "Sending payload to $url..."
try {
    write-host "Header is $headers"
    $response = Invoke-RestMethod -Uri $url -Method POST -Headers $headers -Body $jsonBody
    write-host "response is "$response
    Write-Host " Added user $userId to Governance Group $ggId"
} catch {
    Write-Host "Failed for $ggId "$($_.Exception.Message)
}
}
Error - Failed for 62f05d88-5e64-42c3-9d55-ca6bbdcefa89 The remote server returned an error: (400) Bad Request.