微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Google身份验证流程

我正在尝试编写一个本机应用程序来访问用户谷歌日历.我试图使用谷歌提供的示例来获取身份验证,但它似乎永远不会启动身份验证功能

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);
}

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

解决方法

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐