PowerShell 3: Using Invoke-RestMethod to refresh a new oAuth 2 token

I wanted to translate this code into powershell. Below is the Powershell code to request a refresh token from Google using oAtuh 2.

$CLEINTID="1234567890.apps.googleusercontent.com"
$CLIENTSECRET="aBcDeFgHiJkLmNoPqRsTuVwXyZ"
$REFRESHTOKEN="1/551G1yXUqgkDGnkfFk6ZbjMLMDIMxo3JFc8lY8CAR-Q"

$URL = "https://accounts.google.com/o/oauth2/token"
$Body= 'client_secret={0}&grant_type=refresh_token&refresh_token={1}&client_id={2}' -f $CLIENTSECRET,$REFRESH_TOKEN,$CLEINTID
Invoke-RestMethod -URI $URL -Method Post -Body $Body

Hope that helps someone.

,

One Response to PowerShell 3: Using Invoke-RestMethod to refresh a new oAuth 2 token

  1. Andy Kruta November 1, 2013 at 9:50 am #

    You’ve got a bug in your code there:

    $Body= ‘client_secret={0}&grant_type=refresh_token&refresh_token={1}&client_id={2}’ -f $CLIENTSECRET,$REFRESH_TOKEN,$CLEINTID

    Should be:

    $Body= ‘client_secret={0}&grant_type=refresh_token&refresh_token={1}&client_id={2}’ -f $CLIENTSECRET,$REFRESHTOKEN,$CLEINTID

    Other than that, works a treat! Thanks for sharing!! 😀