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
2
  • 1
    Can you try posting the exact command? From your post it sounds like it would look like this. "Invoke-WebRequest -Uri "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? Commented May 16, 2022 at 21:53
  • 1
    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. Commented May 17, 2022 at 15:37

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

looks like Get-PSAuthorizationString comes from the PSAuth module published in GitHub (github.com/PlagueHO/PSAuth) - can't say I've used it

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.