How do I write a VB or C # GUI client for DotNetOpenAuth?

I am learning how to use the Google Calendar API, which in turn requires me to learn how to use DotNetOpenAuth to access a Google account. I did the provided work samples and wrote working code in the Console to access and manage the Calendar.

Now I want to write a Windows Form Application (in C # or VB) to do the same. I cannot get the OAuth2 process to work in a GUI application. It compiles and runs but doesn't work. Based on what I've seen so far, I've come to the conclusion that the GetAuthorization () function is not being called.

I tried to start the process from a button click, from the constructor and from the Loader method of the form. I've tried in both C # and VB.

public GoogleCal()

    var provider = new NativeApplicationClient(
    provider.ClientIdentifier = "";
    provider.ClientSecret = "yyyyy";

    var auth = new OAuth2Authenticator<NativeApplicationClient>(
                       provider, GetAuthorization);

private 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):
    authCodeText = Microsoft.VisualBasic.Interaction.InputBox(
                                                   "Paste code:", "title", "");

    // Retrieve the access token by using the authorization code:
    return arg.ProcessUserAuthorization(authCodeText, state);


I'm obviously doing something wrong, but I can't figure out what it is. Any ideas?


source to share

1 answer

Using the tutorial at whelkaholism.blogspot I was able to do what I wanted. I took a slightly different approach (in part to leverage the later OAuth2).

Following is the code for a C # form program.

// Add references:
//      DotNetOpenAuth.dll
//      Google.Apis.dll
//      Google.Apis.Authentication.OAth2.dll
//      Newtonsoft.Json.Net35.dll
// plus, add references for whichever Google App(s) you want
//      Google.Apis.Calendar.v3.dll

// form contains at least the following:
//      button1: Button, start the authentication process
//      authCode: TextBox, to recive the auth code from Google
//      button2: Button, complete the authentication prop
//      textBox2: TextBox, multi-line, to display status message and output

// in addition to the libraries required for any forms program, use:
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Util;

// methods not related to authentication deleted for space

namespace GoogleCal
    public partial class GoogleCal : Form
        private static String NL = Environment.NewLine;
        private static IAuthorizationState state;
        private static NativeApplicationClient provider;
        private static OAuth2Authenticator<NativeApplicationClient> auth;
        private static CalendarService calService;

        private void button1_Click(object sender, EventArgs e)
            // clicked to initiate authentication process

            // provider and state are declared above as private static

            provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = "<my id>";
            provider.ClientSecret = "<my secret>";

            // next line changes if you want to access something other than Calendar
            state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = provider.RequestUserAuthorization(state);


        private void button2_Click(object sender, EventArgs e)
            // clicked after Google code is pasted into TextBox to complete authentication process

            // auth and calService are declared above as private static

            auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);

            // create the service object to use in other methods
            calService = new CalendarService(auth);

            textBox2.AppendText("Ready" + NL);

        private IAuthorizationState GetAuthorization(NativeApplicationClient arg)
            // state is declared above as private static
            // authCode is a TextBox on the form
            return arg.ProcessUserAuthorization(authCode.Text, state);




All Articles