I was tasked with spinning up a script for a client which relied on a basic 'GET' request to an API which returned a JSON object which I used info from that to make subsequent calls. This worked great but requirements changed and now I need to send the request with some parameters.
Did some testing in postman and the call works great when I add the query parameters at the end of the Uri (ie. https://test.com/?type=image) but when I try to alter the Uri in the Invoke-WebRequest I'm getting a 'Invoke-RestMethod : Invalid or expired token' error. When I take out the parameters the it works as expected, just with incorrect data.
I have also tried turning the query parameters into a hashtable and as json, and sending it as the body but still get the same error.
I'm at the end of my rope and any insight is appreciated.
what works
$baseUrl = 'https://test.com/api/v2/'
$method = 'GET'
$auth = Get-PSAuthorizationString -Uri $baseUrl -OauthConsumerKey $oauth_consumer_key -OauthConsumerSecret $oauth_consumer_secret -OauthAccessToken $oauth_token -OauthAccessTokenSecret $oauth_token_secret
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", $auth)
$responses = Invoke-RestMethod -Method $method -Headers $headers -Uri $baseUrl
what breaks it
$baseUrl = 'https://test.com/api/v2/?type=image'
$responses = Invoke-RestMethod -Method $method -Headers $headers -Uri $baseUrl
$body = @{}
$body['type']="image"
$responses = Invoke-RestMethod -Method $method -Headers $headers -Uri $baseUrl -body $body