0

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 here.

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:

var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
var service = new CalendarService(auth);
string id = <calendar id>;
Calendar calendar = service.Calendars.Get(id).Fetch();

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 GetAuthorization method:

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);
}

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).

But my problem is the fact that I want this functionality in a WinForms app, not a Console app. I have not been able to find anything on Google's pages regarding this.

What I have so far:

  • User clicks a button, the browser is opened and the user grants access and retrieves the code.
  • User pastes this code into the app.
  • 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.

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.

gosr
  • 4,593
  • 9
  • 46
  • 82

1 Answers1

0

Take a look at the AuthorizationMgr class and in particular at RequestNativeAuthorization method. It uses a LoopbackServerAuthorizationFlow class, which created a tcp listener for you and get the code from the incoming request.

You should also take a look at one of the samples that uses that mechanism (e.g. CreateTask sample)

peleyal
  • 3,472
  • 1
  • 14
  • 25