title: Google Oauth 2 get access token and refresh token from authorization code
body: <p>I got the authorization code with the help of javascript. Now at the server, I need refresh token in exchange. Here is my code.</p>
  
  <pre><code>    byte[] buffer = Encoding.ASCII.GetBytes("code=" + "....."
          + "&amp;client_id=.....&amp;client_secret=...&amp;redirect_uri=https%3A%2F%2Fmysite.com%2Fportal%2F&amp;grant_type=authorization_code");
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
  
      req.Method = "POST";
      req.ContentType = "application/x-www-form-urlencoded";
      req.ContentLength = buffer.Length;
  
      Stream strm = req.GetRequestStream();
      strm.Write(buffer, 0, buffer.Length);
      strm.Close();
  
      HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  </code></pre>
  
  <p>It raises exception <code>Bad request</code> with inner exception as null.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I'm using this in ASP.NET MVC 4 application. Since it is asynchronous operation, I switched to Visual Studio 2013. I modified the code with <code>await</code> : </p>
  
  <pre><code>UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                      GoogleClientSecrets.Load(stream).Secrets,
                          new[] { 
                          GmailService.Scope.GmailCompose, GmailService.Scope.GmailModify, GmailService.Scope.GmailReadonly
                          },
                      "user", 
                      CancellationToken.None
                      ) ;
  </code></pre>
  
  <p>At this statement, it hangs the browser (similar to <a href="https://stackoverflow.com/a/25914745/1323304">this</a> question). I tried putting logs, no exception and no further execution is done.</p>