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