how do you invoke rest method powershell to get access token

Viewed 1534

I'm trying to create a small powershell script to invoke a rest-method to get an access token. I was able to figure out how to use thunder client to get the data I need in a get request, but don't know how to translate that information into a powershell command.

Info is as follows in thunder client to generate a new token:

Grant Type: Client Credentials
Token URL: {{token_url}}
clientID: {clinet_ID}}
clientSecret: {client_secret}}
scope: {{scope}}
send Auth: As Auth Header

In powershell?

2 Answers

Hi I am creator of Thunder Client, since you already using the extension, you can use Code Snippet feature to generate PowerShell code

Enter Client ID, Client secret in Basic Auth Tab, and enter other values as shown in image below.

Thunder Client Powershell code snippet

Something like the following works for me.

Note that I tested with a different auth service, so it's possible you have to tweak it a little to get it to work for you. Let me know if you are having any problems.

$response = Invoke-WebRequest $token_url -Method Post -Body @{
    grant_type = "client_credentials"
    client_id = $clientId
    client_secret = $clientSecret
    scope = $scope
}

You might want to parse the response as an object afterwards, like this:

ConvertFrom-Json $response.Content
Related