服务器端Blazor中的Google日历身份验证

时间:2020-05-28 16:00:26

标签: c# google-calendar-api blazor google-authentication blazor-server-side

正在这里编程的学生迷上了Google的身份验证过程。尝试使用Google身份验证/ Google Calendar API创建日历网络应用。最初的迹象似乎很好,因为我在登录控件上得到了自己的名字,但是我被卡在了日历身份验证部分。经过16小时的精练教程,这就是我想出的...

 public class CalHandler
    {
        private UserCredential Credential { get; set; }       
        private ClaimsPrincipal User { get; set; }
        private CalendarService Service { get; set; }

        public void setUser(ClaimsPrincipal user)
        {
            User = user;
        }


        public void Authorize()
        {
            string[] Scopes = { CalendarService.Scope.Calendar, CalendarService.Scope.CalendarEvents };
            string ApplicationName = "CalendarApp";

            using (var stream = new System.IO.FileStream("appsettings.json", System.IO.FileMode.Open, 
                System.IO.FileAccess.Read))
            {          
                CancellationTokenSource cts = new CancellationTokenSource();
                cts.CancelAfter(TimeSpan.FromSeconds(20));
                CancellationToken ct = cts.Token;

                Credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    ct).Result;
                Service = new CalendarService(new Google.Apis.Services.BaseClientService.Initializer()
                {
                    HttpClientInitializer = Credential,
                    ApplicationName = ApplicationName
                });
            }
        }

        public List<Event> GetEvents(int year, int month, int dim)
        {
            try {
                Authorize();

                EventsResource.ListRequest request = Service.Events.List("primary");
                request.TimeMin = new DateTime(year, month, 1);
                request.TimeMax = new DateTime(year, month, dim);
                request.SingleEvents = true;
                request.MaxResults = 30;
                request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
                List<Event> events = request.Execute().Items.ToList();

                return events;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

当我在Visual Studio中运行所有程序时,一切正常,但是一旦发布到Azure,它就会中断。 GetEvents()始终返回null,并且我的日历上未呈现任何内容。我认为这可能与存储有关,但是我在身份验证方面还没有做很多工作,所以不能确定吗?任何建议或建议可以解决此问题!

0 个答案:

没有答案