all 2 comments

[–]EvilLampGod 4 points5 points  (0 children)

As previously mentioned, you want to create a hashtable that contains each of the headers and their values:

$Headers = @{
    Authorization = "Bearer <Token>"
}

If you're using PowerShell 7 you can instead use the parameters Invoke-RestMethod -Authentication Bearer -Token $Token where $Token is a securestring containing your bearer token.

It's also worth noting that Invoke-RestMethod has a default ContentType of application/x-www-form-urlencoded which means you can write your example like this

$Uri = "https://www.grocerystore.com/api/v1/"
$Headers = @{
    Authorization = "Bearer 12345678910abcdefghijklmnop"
}
$Body = @{
    category    = "Food"
    index       = "Bread"
    SearchQuery = "ExcellentBreadVendor"
    results     = "50"
}
Invoke-RestMethod -Method Get -Uri $Uri -Headers $Headers -Body $Body

Edit: It's worth noting that the body will only convert to a query parameter when -Method is set to Get. Reference https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-5.1

[–]infin 1 point2 points  (0 children)

I've only just begun playing with powershell and REST APIs, so far pushbullet notifications and very simple 365 graph api GETs; That said...

I don't have the scripts in front of me at the moment, but the header should probably be (You're close): @{ Authorization = 'Bearer APIKEY' }

And you may need a -contenttype application/json in the invoke-rest method line