문제

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();
    }
도움이 되었습니까?

해결책 2

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top