【问题标题】:Google Authentication Process谷歌认证流程
【发布时间】:2014-05-14 20:12:36
【问题描述】:

我正在尝试编写一个本地应用程序来访问用户的谷歌日历。我正在尝试使用谷歌提供的示例来获取身份验证,但它似乎从未触发身份验证功能

private void Window_Initialized(object sender, EventArgs e)
{
    var provider = new NativeApplicationClient(
            GoogleAuthenticationServer.Description);
    provider.ClientIdentifier = "<My Client Id here>";
    provider.ClientSecret = "<My Client Secret here";

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

    CalendarService service = new CalendarService();
    CalendarsResource.GetRequest cr = service.Calendars.Get("{primary}");

    if (cr.CalendarId != null)
    {
        Console.WriteLine("Fetching calendar");
        //Google.Apis.Calendar.v3.Data.Calendar c =
            service.Calendars.Get("{primary}").Fetch();
    }
    else
    {
        Console.WriteLine("Service not found");
    }
}

这是我用于身份验证的代码。我从来没有看到控制台写行被发布。

private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
    Console.WriteLine("Authorization Requested");

    IAuthorizationState state = new AuthorizationState(
        new[] { CalendarService.Scopes.Calendar.GetStringValue() });
    state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
    Uri authUri = arg.RequestUserAuthorization(state);

    Process.Start(authUri.ToString());
    // Request authorization from the user and get the code
    string authCode = Console.ReadLine();

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

有没有更好的教程或者我做错了什么?

【问题讨论】:

  • 你能解决这个问题吗,因为我面临同样的问题。

标签: c# oauth-2.0 google-calendar-api google-api-dotnet-client


【解决方案1】:

示例代码已损坏。要使服务使用您的身份验证器,您需要连接它。在示例中,服务和身份验证器之间没有关联。像这样创建服务:

var service = new CalendarService(new BaseClientService.Initializer()
{
    Authenticator = auth
};

查看https://code.google.com/p/google-api-dotnet-client/ 以获得更好的文档/工作代码。

【讨论】:

    【解决方案2】:

    查看新的docs。你需要更换

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

    与

    AuthenticatorFactory.GetInstance().RegisterAuthenticator(
        () => new OAuth2Authenticator(provider, GetAuthentication));
    

    正如评论所说,当你调用var service = new CalendarService()时,会自动调用之前注册的验证器。

    【讨论】:

    • 需要添加哪些引用才能使用它?每当我在 Visual Studio 中添加这段代码时说它找不到工厂并且也没有解决操作。
    • 如示例中的using System; using System.Diagnostics; using DotNetOpenAuth.OAuth2; using Google.Apis.Authentication; 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;,您可以在code.google.com/p/google-api-dotnet-client/wiki/…下载DLL
    【解决方案3】:

    我认为上述方式与旧版谷歌日历DLL有关。有谁知道新版 Google 日历的文档,即 V 1.8.1.82。 Google 从来没有为 .NET 开发者提供好的文档。s

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2014-02-22
      • 2011-05-11
      • 2014-10-05
      相关资源
      最近更新 更多