0

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

1 Answer 1

1

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 [...]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.