使用不同的用户/资源所有者登录

时间:2013-03-14 23:39:17

标签: oauth-2.0 google-api-dotnet-client

我正在尝试编写一个在Google日历中创建条目的工具。 在关注谷歌文档并在api控制台中创建客户端标识符/秘密之后,我设法将一个正确验证的客户端放在一起并显示我注册的谷歌日历。现在对我来说,看起来我的谷歌帐户以某种方式绑定到我的客户端标识符/秘密。我想知道的是:我如何更改身份验证过程,以便此工具的其他用户可以输入他的google-id并访问他的日历?

编辑:换句话说(在RFC中使用):我希望使资源所有者部分可编辑,同时保持客户端部分不变。但我的例子虽然有效,但却将客户和资源所有者联系在一起。

这是我的应用程序到目前为止工作正常:

    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 个答案:

答案 0 :(得分:0)

提示用户输入其ClientIdentifier和ClientSecret,然后将这些值传递给Connect方法。

答案 1 :(得分:0)

我自己解决了这个问题。

问题是,我通常总是连接谷歌,因为在我的应用程序重定向到谷歌获取访问令牌之前我没有从谷歌退出,谷歌自动为我的帐户生成访问令牌 - 跳过输入表单出现的部分,任何人都可以输入他/她的用户凭据,让google为他/她的帐户生成访问令牌。