Ash
Ash

Reputation: 3

Authorization Error while making API using API token

I am new to API calls, I am using Powershell for API calls and i have a api token for my application

The application clearly mentions we to create custom http headers to send authenticated requests:

Format is below

Authorization: ApiToken 'API_TOKEN_VALUE' 

When I run the below command in powershell:

Invoke-RestMethod -uri "https://custom.net/users" -H "Authorization: ApiToken avadhohofgsdgdisbgsdhgsfd" 

I get this error as response

Invoke-RestMethod : {"errors":[{"code":4010010,"detail":null,"title":"Authentication Failed"}]}
At line:1 char:1
+ Invoke-RestMethod -Uri "https://custom.net/users
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod]
   , WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodComm
   and

Any idea how should i define my http header for successful authentication?

Thank you very much

Upvotes: 0

Views: 1476

Answers (1)

Avshalom
Avshalom

Reputation: 8889

You are passing a string instead of IDictionary, see the documentation for the Invoke-RestMethod cmdlet..,

Specifically the -Headers parameter:

-Headers <IDictionary>

        Type                    IDictionary
        Position                Named
        ...                     ...

So, change your Authorization header to an Hashtable (it's a dictionary)

$TOKEN_VALUE = 'avadhohofgsdgdisbgsdhgsfd'
$authHeader = @{
'Authorization'= "ApiToken $TOKEN_VALUE"
}

Invoke-RestMethod -uri "https://custom.net/users" -H $authHeader [...]

Upvotes: 1

Related Questions