1

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
  • 1
    Can you try posting the exact command? From your post it sounds like it would look like this. "Invoke-WebRequest -Uri "https://stackoverflow.com/?type=image"". Which seems to work just fine. It could be that the endpoint you are requesting acts differently. Or it could be that it should now be a POST with a body instead of a GET request? – Zucchini May 16 '22 at 21:53
  • Updated post to add code sample. It is indeed a GET request. I'm thinking the issue has something to do with how the Invoke-RestMethod does URI encoding but I can't seem to find anything definitive. – LightningBug May 17 '22 at 15:37

1 Answers1

1

Looks like you are inadvertently using $baseUrl in 2 different places, when requesting your token and when invoking your web request.

From your error message, I guess the authorization service does not tolerate the extra parameter.

Try this simple change:

$authUrl = 'https://test.com/api/v2/'
$baseUrl = 'https://test.com/api/v2/?type=image'
$method = 'GET'
$auth = Get-PSAuthorizationString -Uri $authUrl -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

Hope this helps!

RohanD
  • 11
  • 2
  • looks like Get-PSAuthorizationString comes from the PSAuth module published in GitHub (https://github.com/PlagueHO/PSAuth) - can't say I've used it – RohanD Jul 06 '23 at 12:17