AuthorizeAsync() публичный Метод

Asynchronously authorizes the installed application to access user's protected data. It gets the user identifier by calling to Google.Apis.Auth.OAuth2.Mvc.FlowMetadata.GetUserId and then calls to Google.Apis.Auth.OAuth2.AuthorizationCodeWebApp.AuthorizeAsync.
public AuthorizeAsync ( CancellationToken taskCancellationToken ) : Task
taskCancellationToken System.Threading.CancellationToken Cancellation token to cancel an operation
Результат Task
Пример #1
0
        /// <summary>
        /// Authorizes the specified user id.
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task{System.Boolean}.</returns>
        public async Task AuthorizeAsync(Controller controller, CancellationToken cancellationToken)
        {
            Authorized = false;

            if (!UseGoogleCalendar)
                return;

            try
            {
                using (var appFlowMetadata = new AppFlowMetadata(HttpContext.Current.Request.PhysicalApplicationPath,GoogleCredentialsJson))
                {
                    if (!appFlowMetadata.Valid)
                        throw new Exception("AppFlowMetadata is not valid.");

                    var authorizationCodeMvcApp = new AuthorizationCodeMvcApp(controller, appFlowMetadata);
                    AuthorizationCodeWebApp.AuthResult result = await authorizationCodeMvcApp.AuthorizeAsync(cancellationToken);
                    if (result.Credential == null)
                    {
                        NeedRedirectToGoogle = true;
                        RedirectUri = result.RedirectUri;

                        Cookie.SetNewCookie(Cookie.GoogleCredentialsJsonCookieKey, GoogleCredentialsJson, controller.HttpContext.Response);
                        return;
                    }

                    var initializer = new BaseClientService.Initializer
                    {
                        HttpClientInitializer = result.Credential,
                        ApplicationName = Properties.Settings.Default.WebTitle
                    };
                    _calendarService = new CalendarService(initializer);

                    bool calendarValid = !String.IsNullOrEmpty(GoogleCalendarId)
                                             ? _calendarService.CalendarList.List().Execute().Items.Any(
                                                 cle => cle.Id == GoogleCalendarId)
                                             : _calendarService.CalendarList.List().Execute().Items.Any();
                    if (!calendarValid)
                        throw new Exception("Calendar Id is not valid.");                    
                }
            }
            catch (Exception e)
            {
                Logger.SetLog(e);
                return;
            }

            Authorized = true;
        }