title: Using PowerShell to get OAuth2 Authorization Code for Google Sheets
body: <p>I'm trying to automate the process of acquiring an <code>OAuth2</code> Authorization Code for use with Google Sheets.</p>
  
  <p>I've created the Google Project and registered it with the redirect URI of <code>http://localhost/oauth2callback</code></p>
  
  <p>I've successfully gotten the Authorization Code manually by entering this URL into Chrome:</p>
  
  <pre><code>https://accounts.google.com/o/oauth2/v2/auth
    ?redirect_uri=http%3A%2F%2Flocalhost%2Foauth2callback
    &amp;response_type=code
    &amp;client_id=##CLIENTID##
    &amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets
    &amp;access_type=offline
    &amp;include_granted_scopes=true
    &amp;state=state_parameter_passthrough_value
  </code></pre>
  
  <p>...and then extracting the Authorization Code by copying it out of the resulting URL shown in Chrome:</p>
  
  <pre><code>http://localhost/oauth2callback
    ?state=state_parameter_passthrough_value
    &amp;code=##AUTHCODE##
    &amp;scope=https://www.googleapis.com/auth/spreadsheets#
  </code></pre>
  
  <p>My attempts to replicate this process with PowerShell are failing miserably.  I think I'm missing some fundamental understanding of how all this HTTP redirect stuff works.</p>
  
  <p>I've tried several variations on the theme, including taking the first redirect result and submitting it with another <code>Invoke-WebRequest</code> call, but no matter what I do, I only get a value back on the first call, and only if I set <code>-MaximumRedirection</code> to <code>0</code> -- and what I get back doesn't help me:</p>
  
  <pre><code>Function getAuthorizationCode($cliids) {
      $auturl = "https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=http`%3A`%2F`%2Flocalhost`%2Foauth2callback`&amp;response_type=code`&amp;client_id=$cliids`&amp;scope=https`%3A`%2F`%2Fwww.googleapis.com`%2Fauth`%2Fspreadsheets`&amp;access_type=offline`&amp;include_granted_scopes=true`&amp;state=state_parameter_passthrough_value";
      #$autrsp = Invoke-WebRequest -UseBasicParsing -Uri $auturl -UseDefaultCredentials -TimeoutSec 180 -ErrorAction SilentlyContinue;
      $autrsp = Invoke-WebRequest -UseBasicParsing -Uri $auturl -UseDefaultCredentials -TimeoutSec 180 -MaximumRedirection 0 -ErrorAction Ignore;
      $autrdi = "";
      if ($autrsp.StatusCode -ge 300 -and $autrsp.StatusCode -lt 400) {
          $autrdi = $autrsp.Headers.Location;
      }   
      return $autrdi;
  }
  # Force main variables to be non-global
  Function myMain($arglis) {
      $cliids = "##CLIENTID##";
      getAuthorizationCode $cliids;
  }
  # Force main variables to be non-global
  myMain $Args;
  </code></pre>
  
  <p>This results in:</p>
  
  <pre><code>https://accounts.google.com/ServiceLogin
    ?passive=1209600
    &amp;continue=https://accounts.google.com/o/oauth2/v2/auth
      ?redirect_uri%3Dhttp://localhost/oauth2callback
      %26response_type%3Dcode
      %26client_id%3D##CLIENTID##
      %26scope%3Dhttps://www.googleapis.com/auth/spreadsheets
      %26access_type%3Doffline
      %26include_granted_scopes%3Dtrue
      %26state%3Dstate_parameter_passthrough_value
      %26from_login%3D1
      %26as%3D##LOGINCODE##
    &amp;oauth=1
    &amp;sarp=1
    &amp;scc=1
  </code></pre>
  
  <p>Nothing in that resulting redirect value is the Authorization Code, and none of my efforts to use the information returned has gotten me an Authorization Code, either.</p>
  
  <p>I've pored over a bunch of Google and StackOverflow search results and either I'm searching for the wrong things or not understanding what I'm finding.</p>
  
  <p>I'm stumped.  Can someone help me figure out what direction I need to steer here?</p>
  
  <p>Thanks!</p>