401 Unauthorized Error in PowerShell Calling IDN API

I have a powershell script which will generate access token based on provied client ID and client Secret which will be used to invoke Search API. I run in the script in IQService Server and able to generate token but while using the same token in API header, I got 401 error.

Would appreciate if anyone can spot the issue. Attached screenshot for reference.

Not sure what some of the other variables are before your screenshot, but here is my version of /v3/search in PowerShell. It looks like the access token is good. If this doesn’t work can you try outputting the $searchIdentities object to troubleshoot if the token is in there correctly?

function Search-IDN {
    param (
        [String]$query = "*",
        [String]$indices = "identities",
        [int]$limit = 250,
        [int]$offset = 0,
        [String]$org = "",
        [String]$searchAfter = "",
        [String]$token = ""
    )

    $results = @()
    $response = $null

    do {
        $body = @{
            indices     = @($indices)
            query       = @{
                query = $query
            }
            sort        = @("id")
            searchAfter = @($searchAfter)
        }
        $params = @{
            method      = "POST"
            uri         = "https://$($org).api.identitynow.com/v3/search?limit=$limit"
            body        = (ConvertTo-Json $body)
            headers     = @{Authorization = "Bearer $token" }
            ContentType = "application/json"
        }

        $response = Invoke-RestMethod @params
        $results += $response
        if ($response.count -gt 1) {
            $searchAfter = $response[$response.count - 1].id
        }
    } until ($response.count -lt $limit)

    return $results
}


Hi @ethompson Thank You for your response.
I use the same part of relayed code in my already existing code still i am getting JWT required error.
Below is the screenshot for the same.

Hey @swapnasarit,

It would help other community members in supporting you if you could share the actually code block here rather than a screenshot. Is that possible?

@ethompson @jordan_violet

I was able to solve the issue but can’t figure out the actual issue with my code. I assume, I generated access token and reused the value in second phase and while doing the same I used “echo” command to write the output which writes value along with statement. I replaced “echo” with “Write to host” command and issue solved.

1 Like

That’s great to hear! Thanks for coming back to share the solution :slight_smile:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.