0

i am trying to write a tool that creates entries in the google calendar. after following the google docs and creating an client-identifier/secret in the api console, i managed to put together a client that authenticates correctly and shows my registered google calendars. right now for me it looks like my google-account is somehow tied to my client-identifier/secret. what i want to know is: how can i change the auth process so that it is possible for an other user of this tool to enter his google-id and get access to his calendars?

EDIT: in other words (used in the RFC): I want make the resource owner-part editable while leaving the client-part unchanged. but my example, although working, ties together client and resource owner.

here is my app that works fine so far:

    public void Connect()
    {
        var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
        provider.ClientIdentifier = "123456123456.apps.googleusercontent.com";
        provider.ClientSecret = "nASdjKlhnaxEkasDhhdfLklr";
        var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
        var service = new CalendarService(auth);

        //Events instances = service.Events.Instances("primary", "recurringEventId").Fetch();
        var list = service.CalendarList.List().Fetch();

        foreach (var itm in list.Items)
            Console.WriteLine(itm.Summary);
    }

    private static readonly byte[] AditionalEntropy = { 1, 2, 3, 4, 5 };

    private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
    {
        var state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
        state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);

        var refreshToken = LoadRefreshToken();
        if (!String.IsNullOrWhiteSpace(refreshToken))
        {
            state.RefreshToken = refreshToken;

            if (arg.RefreshToken(state))
                return state;
        }

        var authUri = arg.RequestUserAuthorization(state);

        // Request authorization from the user (by opening a browser window):
        Process.Start(authUri.ToString());
        var frm = new FormAuthCodeInput();
        frm.ShowDialog();

        // Retrieve the access token by using the authorization code:
        var auth = arg.ProcessUserAuthorization(frm.txtAuthCode.Text, state);
        StoreRefreshToken(state);
        return auth;
    }

    private static string LoadRefreshToken()
    {
        try
        {
            return Encoding.Unicode.GetString(ProtectedData.Unprotect(Convert.FromBase64String(Properties.Settings.Default.RefreshToken), AditionalEntropy, DataProtectionScope.CurrentUser));
        }
        catch
        {
            return null;
        }
    }

    private static void StoreRefreshToken(IAuthorizationState state)
    {
        Properties.Settings.Default.RefreshToken = Convert.ToBase64String(ProtectedData.Protect(Encoding.Unicode.GetBytes(state.RefreshToken), AditionalEntropy, DataProtectionScope.CurrentUser));
        Properties.Settings.Default.Save();
    }
gofrm
  • 364
  • 2
  • 15
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 14 '13 at 23:42

2 Answers2

0

Prompt the user to enter their ClientIdentifier and ClientSecret, then pass these values to your Connect method.

  • i don't want to change the client-identifier and -secret, these values identify my program. i want to let other users (resource owners) use my program (client) to access their resources at google (resource server) – gofrm Mar 15 '13 at 08:35
0

i solved the problem myself.

the problem was, that i'm usually always connected to google and because i did't log out from google before my app redirected to google to get the access-token, google automatically generated the access-token for my account - skipping the part where an input-form appears where anyone could enter his/her user-credentials to let google generate an access-token for his/her account.

gofrm
  • 364
  • 2
  • 15