interacting with an API using powershell using an API-KEY

rob m 256 Reputation points
2023-01-27T16:07:58.8666667+00:00

I am wanting to retrive data from an API using powershell

Ideally it would work the same way this curl command works , any suggestions?

[root@svd-nmrobm01 ~]# curl -d '{"sam_account_name":"smithm"}' -H "Content-Type: application/json" -H "Authorization: Bearer <API KEY>" -X POST https://people.test.ca/api/v3/uids/

{"sam_account_name":"smithm","uid":39730}

[root@svd-nmrobm01 ~]#

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,583 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 54,806 Reputation points
    2023-01-27T16:15:02.5+00:00

    What is your actual problem? APIs that require an API key generally have that API key passed in the HTTP header as a custom header value (specified by the API docs). Therefore you'd just use the standard -H parameter like you're already using for the content type. There is nothing special about the key. You don't use the authorization header.

    $apiKey = "My api key"
    curl ... -H "Company-Api-Key $apiKey"
    

    If the API you're calling really does try to pass off an API key as the bearer token then they are really messed up. But the code works the same.

    $apiKey = "My api key"
    curl ... -H "Authorization: Bearer $apiKey"
    

    But bear (in pun intended) mind that the authorization header is predefined and requires the format given. If the api key has spaces or other "invalid" characters then that will mess it up. Most of the time the actual value is encoded to prevent this. Read the docs for the API you're calling to determine how to properly send them the authentication information.

    0 comments No comments

  2. Rich Matheisen 46,811 Reputation points
    2023-01-27T16:30:13.2133333+00:00

    Use the Invoke-WebRequest cmdlet.


  3. Rich Matheisen 46,811 Reputation points
    2023-01-28T02:43:30.3333333+00:00

    I think this is what you need to do:

    $header = @{
        "Content-Type"="application/json"
       }
    $body = @{
        "Authorization"="Bearer <API KEY>"
    } | ConvertTo-Json
    
    Invoke-WebRequest -Uri http://192.168.1.202 -Method POST -Headers $header -Body $body
    
    

    This is what's sent to the web server:

    POST http://192.168.1.202/ HTTP/1.1
    Content-Type: application/json
    User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.19041.2364
    Host: 192.168.1.202
    Content-Length: 56
    
    {
        "Authorization":  "Bearer \u003cAPI KEY\u003e"
    }
    
    

    If that doesn't work, you can install Fiddler (https://www.telerik.com/fiddler) on your client machine and capture the web traffic for examination. You can use "curl" and see exactly what it's sending if the above code doesn't work. Then make the necessary changes to the Invoke-WebRequest until it looks the same.

    Fiddler's pulled my bacon out of the fire more than once.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.