title: Google API OAuth 2: Use authorization code as a string with Google's .NET libraries
body: <p>My WinForms app needs to access one of the Google API's (Calendar). For this, the app needs to have authorization, and Google has provided OAuth 2 for this purpose. I've read everything on their docs site <a href="https://developers.google.com/accounts/docs/OAuth2InstalledApp" rel="nofollow">here</a>.</p>
  
  <p>From another documentation page on Google I learned how to get the authorization key via a browser request. This takes place in a Console C# application. What it does is:</p>
  
  <pre><code>var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
  var auth = new OAuth2Authenticator&lt;NativeApplicationClient&gt;(provider, GetAuthorization);
  var service = new CalendarService(auth);
  string id = &lt;calendar id&gt;;
  Calendar calendar = service.Calendars.Get(id).Fetch();
  </code></pre>
  
  <p>At the last line, a browser window is opened with a Google page asking me to allow the app access to my Google account. In the console application, a ReadLine() is waiting for input. This comes from the <code>GetAuthorization</code> method:</p>
  
  <pre><code>private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
  {
      // Get the auth URL:
      IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
      state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
      Uri authUri = arg.RequestUserAuthorization(state);
  
      // Request authorization from the user (by opening a browser window):
      Process.Start(authUri.ToString());
      Console.Write("  Authorization Code: ");
      string authCode = Console.ReadLine();
      Console.WriteLine();
  
      // Retrieve the access token by using the authorization code:
      return arg.ProcessUserAuthorization(authCode, state);
  }
  </code></pre>
  
  <p>So I grant my app access to my Gmail account, and I get a code in return. This code I paste back into the console window, and the rest of the app does its work as it should (in my case, making a new Calendar event).</p>
  
  <p>But my problem is the fact that I want this functionality in a WinForms app, not a Console app. I have <em>not</em> been able to find anything on Google's pages regarding this.</p>
  
  <p>What I have so far:</p>
  
  <ul>
  <li>User clicks a button, the browser is opened and the user grants access and retrieves the code.</li>
  <li>User pastes this code into the app.</li>
  <li>Another button is clicked, and this is where I would like to use the user-entered code for the authorization process. This is a string, and I don't know how to combine this with all the authentication methods written in the top of this post.</li>
  </ul>
  
  <p>I have a feeling it could be possible with the use of Google's REST client instead of the native .NET libraries, but I sincerely want to use the .NET libraries instead of REST.</p>