ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – Google身份验证流程

2019-07-04 11:54:36  阅读:189  来源: 互联网

标签:c oauth-2-0 google-calendar-api google-api-dotnet-client


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

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/以获得更好的文档/工作代码.

标签:c,oauth-2-0,google-calendar-api,google-api-dotnet-client
来源: https://codeday.me/bug/20190704/1376838.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有