Zz11
Zz11

Reputation: 191

Need to get required http post

I am following Huddle Api instructions to get the Access Token. I am using powershell to post the method which is as follows:

POST /token HTTP/1.1
Host: login.huddle.net
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&client_id=s6BhdRkqt&redirect_uri=MyAppServer.com/receiveAuthCode&code=i1WsRn1uB1

Powershell Command which I am using is:

$body = { '@grant_type' = 'authorization_code'; client_id = 'xxxxx';
           redirect_uri = 'myAppServer.com'; code = '123abcdef' } 

Invoke-WebRequest -Uri "login.huddle.com" -ContentType "application/x-www-form-urlencoded" -Method Post

This works and I get the response of "200 OK" and also shows the activation of Access Token. How would I retrieve the Access Token number. For example, I need the output as they mentioned in instruction which is:

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store

{
 "access_token":"S1AV32hkKG",
 "expires_in":300,
 "refresh_token":"8xLOxBtZp8"
}

I think it has something to do ContentType. So I did try, "application/Json" but that was not it. Any suggestions?

Upvotes: 0

Views: 1004

Answers (2)

FoxDeploy
FoxDeploy

Reputation: 13537

You're using the wrong cmdlet. Since you mentiond getting back values for StatusCode, Content, RawContent, etc, that tells us that you're using Invoke-WebRequest. This cmdlets awesome...but not for working with APIs, which are commonly REST formatted and use JSON. IWR can handle the request but you have to dig into the $Response.Content and convert from JSON.

Instead of Invoke-WebRequest, try using Invoke-RestMethod. It's likely that you are getting the AccessCode returned, but as a JSON formatted property. Invoke-RestMethod will natively parse and convert JSON into PowerShell objects. You can just sub it in for Invoke-WebRequest and it should just work.

Invoke-RestMethod -Uri "login.huddle.com" -ContentType "application/x-www-form-urlencoded" -Method Post -body $body

Upvotes: 1

Patrick Murphy
Patrick Murphy

Reputation: 2329

If you use Invoke-RestMethod you can set the response when making the call

$response = Invoke-RestMethod -Uri "login.huddle.com" -ContentType "application/x-www-form-urlencoded" -Method Post"

then $response.access_token or $response.expires_in or $response.refresh_token

Upvotes: 0

Related Questions