fixes the calendar choice
This commit is contained in:
@ -10,7 +10,6 @@ using Microsoft.Extensions.OptionsModel;
|
|||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
|
||||||
using Microsoft.Extensions.Localization;
|
using Microsoft.Extensions.Localization;
|
||||||
using Yavsc.Models.Workflow;
|
using Yavsc.Models.Workflow;
|
||||||
using Yavsc.Models.Identity;
|
using Yavsc.Models.Identity;
|
||||||
@ -272,20 +271,12 @@ namespace Yavsc.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> SetGoogleCalendar(string returnUrl)
|
public async Task<IActionResult> SetGoogleCalendar(string returnUrl, string pageToken)
|
||||||
{
|
{
|
||||||
try
|
return View(new SetGoogleCalendarViewModel {
|
||||||
{
|
ReturnUrl = returnUrl,
|
||||||
ViewBag.Calendars = await _calendarManager.GetCalendarsAsync(User.GetUserId());
|
Calendars = await _calendarManager.GetCalendarsAsync(User.GetUserId(), pageToken)
|
||||||
}
|
});
|
||||||
catch (WebException ex)
|
|
||||||
{
|
|
||||||
// a bug
|
|
||||||
_logger.LogError("Google Api error");
|
|
||||||
_logger.LogError("Code: "+ex.HResult+"\n"+ ex.Message);
|
|
||||||
return RedirectToAction("LinkLogin", new { provider = "Google" });
|
|
||||||
}
|
|
||||||
return View(new SetGoogleCalendarViewModel { ReturnUrl = returnUrl });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost, ValidateAntiForgeryToken]
|
[HttpPost, ValidateAntiForgeryToken]
|
||||||
|
@ -33,10 +33,9 @@ namespace Yavsc.Helpers
|
|||||||
using Yavsc.Models.Calendar;
|
using Yavsc.Models.Calendar;
|
||||||
using Google.Apis.Auth.OAuth2;
|
using Google.Apis.Auth.OAuth2;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using Google.Apis.Auth.OAuth2.Flows;
|
|
||||||
using Microsoft.AspNet.Identity.EntityFramework;
|
using Microsoft.AspNet.Identity.EntityFramework;
|
||||||
using Yavsc.Services;
|
using Yavsc.Services;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Google helpers.
|
/// Google helpers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -86,25 +85,16 @@ namespace Yavsc.Helpers
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<IdentityUserLogin<string>> GetGoogleUserLoginAsync(
|
public static async Task<IdentityUserLogin<string>> GetGoogleUserLoginAsync(
|
||||||
this UserManager<ApplicationUser> userManager,
|
this ApplicationDbContext context,
|
||||||
ApplicationDbContext context,
|
|
||||||
string yavscUserId)
|
string yavscUserId)
|
||||||
{
|
{
|
||||||
var user = await userManager.FindByIdAsync(yavscUserId);
|
var user = context.Users.FirstOrDefaultAsync(u=>u.Id==yavscUserId);
|
||||||
|
if (user==null) return null;
|
||||||
var googleLogin = await context.UserLogins.FirstOrDefaultAsync(
|
var googleLogin = await context.UserLogins.FirstOrDefaultAsync(
|
||||||
x => x.UserId == yavscUserId && x.LoginProvider == "Google"
|
x => x.UserId == yavscUserId && x.LoginProvider == "Google"
|
||||||
);
|
);
|
||||||
return googleLogin;
|
return googleLogin;
|
||||||
}
|
}
|
||||||
public static UserCredential GetGoogleCredential(string googleUserLoginKey)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(googleUserLoginKey))
|
|
||||||
throw new InvalidOperationException("No Google login");
|
|
||||||
var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer());
|
|
||||||
throw new NotImplementedException();
|
|
||||||
// TokenResponse resp = flow. ;
|
|
||||||
// return new UserCredential(flow, googleUserLoginKey, resp);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<Period[]> GetFreeTime (this ICalendarManager manager, string calId, DateTime startDate, DateTime endDate)
|
public static async Task<Period[]> GetFreeTime (this ICalendarManager manager, string calId, DateTime startDate, DateTime endDate)
|
||||||
{
|
{
|
||||||
|
@ -125,28 +125,7 @@ namespace Yavsc.Models
|
|||||||
|
|
||||||
public DbSet<Service> Services { get; set; }
|
public DbSet<Service> Services { get; set; }
|
||||||
public DbSet<Product> Products { get; set; }
|
public DbSet<Product> Products { get; set; }
|
||||||
public Task ClearTokensAsync()
|
|
||||||
{
|
|
||||||
Tokens.RemoveRange(this.Tokens);
|
|
||||||
SaveChanges(null);
|
|
||||||
return Task.FromResult(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task DeleteTokensAsync(string email)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(email))
|
|
||||||
{
|
|
||||||
throw new ArgumentException("email MUST have a value");
|
|
||||||
}
|
|
||||||
|
|
||||||
var item = this.Tokens.FirstOrDefault(x => x.UserId == email);
|
|
||||||
if (item != null)
|
|
||||||
{
|
|
||||||
Tokens.Remove(item);
|
|
||||||
SaveChanges(email);
|
|
||||||
}
|
|
||||||
return Task.FromResult(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task<OAuth2Tokens> GetTokensAsync(string googleUserId)
|
public Task<OAuth2Tokens> GetTokensAsync(string googleUserId)
|
||||||
{
|
{
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
using Google.Apis.Requests;
|
|
||||||
using Google.Apis.Services;
|
|
||||||
using Yavsc.Models.Google;
|
|
||||||
|
|
||||||
namespace Yavsc.Services.GoogleApis
|
|
||||||
{
|
|
||||||
public class CalendarClient : ClientServiceRequest<Resource>
|
|
||||||
{
|
|
||||||
public CalendarClient(IClientService service) : base (service) {
|
|
||||||
|
|
||||||
}
|
|
||||||
public override string HttpMethod
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return "POST";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public string calendarId
|
|
||||||
{
|
|
||||||
get; set;
|
|
||||||
}
|
|
||||||
public override string MethodName
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return "calendar.events.insert";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string RestPath
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return "calendars/{calendarId}/events";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -28,12 +28,18 @@ using Google.Apis.Auth.OAuth2;
|
|||||||
using Google.Apis.Util.Store;
|
using Google.Apis.Util.Store;
|
||||||
using Google.Apis.Calendar.v3;
|
using Google.Apis.Calendar.v3;
|
||||||
using Google.Apis.Calendar.v3.Data;
|
using Google.Apis.Calendar.v3.Data;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Google.Apis.Http;
|
||||||
|
using Google.Apis.Services;
|
||||||
|
using Microsoft.AspNet.Authentication.OAuth;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
|
|
||||||
namespace Yavsc.Services
|
namespace Yavsc.Services
|
||||||
{
|
{
|
||||||
using System.Collections.Generic;
|
using System.Threading;
|
||||||
using System.Linq;
|
using Google.Apis.Auth.OAuth2.Flows;
|
||||||
using Google.Apis.Services;
|
using Newtonsoft.Json;
|
||||||
using Yavsc.Helpers;
|
using Yavsc.Helpers;
|
||||||
using Yavsc.Models;
|
using Yavsc.Models;
|
||||||
using Yavsc.Models.Calendar;
|
using Yavsc.Models.Calendar;
|
||||||
@ -43,164 +49,204 @@ namespace Yavsc.Services
|
|||||||
/// Google Calendar API client.
|
/// Google Calendar API client.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class CalendarManager : ICalendarManager
|
public class CalendarManager : ICalendarManager
|
||||||
{
|
{
|
||||||
public class ExpiredTokenException : Exception {}
|
public class ExpiredTokenException : Exception { }
|
||||||
protected static string scopeCalendar = "https://www.googleapis.com/auth/calendar";
|
protected static string scopeCalendar = "https://www.googleapis.com/auth/calendar";
|
||||||
private string _ApiKey;
|
private string _ApiKey;
|
||||||
|
private IAuthorizationCodeFlow _flow;
|
||||||
|
private readonly UserManager<ApplicationUser> _userManager;
|
||||||
|
|
||||||
private readonly UserManager<ApplicationUser> _userManager;
|
ApplicationDbContext _dbContext;
|
||||||
|
|
||||||
ApplicationDbContext _dbContext;
|
IDataStore _dataStore;
|
||||||
ILogger _logger;
|
ILogger _logger;
|
||||||
|
|
||||||
public CalendarManager(IOptions<GoogleAuthSettings> settings,
|
public CalendarManager(IOptions<GoogleAuthSettings> settings,
|
||||||
UserManager<ApplicationUser> userManager,
|
UserManager<ApplicationUser> userManager,
|
||||||
ApplicationDbContext dbContext,
|
ApplicationDbContext dbContext,
|
||||||
ILoggerFactory loggerFactory)
|
IDataStore dataStore,
|
||||||
{
|
ILoggerFactory loggerFactory)
|
||||||
|
{
|
||||||
_ApiKey = settings.Value.ApiKey;
|
_ApiKey = settings.Value.ApiKey;
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_logger = loggerFactory.CreateLogger<CalendarManager>();
|
_logger = loggerFactory.CreateLogger<CalendarManager>();
|
||||||
}
|
_dataStore = dataStore;
|
||||||
|
_flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
|
||||||
|
{
|
||||||
|
ClientSecrets = new ClientSecrets
|
||||||
|
{
|
||||||
|
ClientId = Startup.GoogleSettings.ClientId,
|
||||||
|
ClientSecret = Startup.GoogleSettings.ClientSecret
|
||||||
|
},
|
||||||
|
Scopes = new[] { scopeCalendar },
|
||||||
|
DataStore = dataStore
|
||||||
|
});
|
||||||
|
_logger.LogWarning($"Using Google data store from "+JsonConvert.SerializeObject(_dataStore));
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The get cal list URI.
|
/// The get cal list URI.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected static string getCalListUri = "https://www.googleapis.com/calendar/v3/users/me/calendarList";
|
protected static string getCalListUri = "https://www.googleapis.com/calendar/v3/users/me/calendarList";
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The get cal entries URI.
|
/// The get cal entries URI.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected static string getCalEntriesUri = "https://www.googleapis.com/calendar/v3/calendars/{0}/events";
|
protected static string getCalEntriesUri = "https://www.googleapis.com/calendar/v3/calendars/{0}/events";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The date format.
|
/// The date format.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static string dateFormat = "yyyy-MM-ddTHH:mm:ss";
|
private static string dateFormat = "yyyy-MM-ddTHH:mm:ss";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The time zone. TODO Fixme with machine time zone
|
/// The time zone. TODO Fixme with machine time zone
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private string timeZone = "+01:00";
|
private string timeZone = "+01:00";
|
||||||
|
|
||||||
private readonly IDataStore dataStore = new FileDataStore(GoogleWebAuthorizationBroker.Folder);
|
/// <summary>
|
||||||
|
/// Gets the calendar list.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The calendars.</returns>
|
||||||
|
/// <param name="userId">Yavsc user id</param>
|
||||||
|
public async Task<CalendarList> GetCalendarsAsync(string userId, string pageToken)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(userId))
|
||||||
|
throw new Exception("the user id is not specified");
|
||||||
|
var service = await CreateUserCalendarServiceAsync(userId);
|
||||||
|
CalendarListResource.ListRequest calListReq = service.CalendarList.List ();
|
||||||
|
calListReq.PageToken = pageToken;
|
||||||
|
return calListReq.Execute ();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private ServiceAccountCredential GetServiceAccountCredential()
|
||||||
/// <summary>
|
{
|
||||||
/// Gets the calendar list.
|
var creds = GoogleHelpers.GetCredentialForApi(new string[] { scopeCalendar });
|
||||||
/// </summary>
|
if (creds == null)
|
||||||
/// <returns>The calendars.</returns>
|
throw new InvalidOperationException("No credential");
|
||||||
/// <param name="userId">Yavsc user id</param>
|
return creds;
|
||||||
public async Task<CalendarList> GetCalendarsAsync (string userId)
|
}
|
||||||
{
|
/// <summary>
|
||||||
var service = new CalendarService();
|
/// Gets a calendar event list, between the given dates.
|
||||||
|
/// </summary>
|
||||||
var listRequest = service.CalendarList.List();
|
/// <returns>The calendar.</returns>
|
||||||
|
/// <param name="calid">Calendar identifier.</param>
|
||||||
return await listRequest.ExecuteAsync();
|
/// <param name="mindate">Mindate.</param>
|
||||||
}
|
/// <param name="maxdate">Maxdate.</param>
|
||||||
|
/// <param name="cred">credential string.</param>
|
||||||
|
public async Task<Events> GetCalendarAsync(string calid, DateTime mindate, DateTime maxdate)
|
||||||
private ServiceAccountCredential GetServiceAccountCredential()
|
{
|
||||||
{
|
var service = await GetServiceAsync();
|
||||||
var creds = GoogleHelpers.GetCredentialForApi(new string[]{scopeCalendar});
|
|
||||||
if (creds==null)
|
|
||||||
throw new InvalidOperationException("No credential");
|
|
||||||
|
|
||||||
return creds;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a calendar event list, between the given dates.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The calendar.</returns>
|
|
||||||
/// <param name="calid">Calendar identifier.</param>
|
|
||||||
/// <param name="mindate">Mindate.</param>
|
|
||||||
/// <param name="maxdate">Maxdate.</param>
|
|
||||||
/// <param name="cred">credential string.</param>
|
|
||||||
public async Task<Events> GetCalendarAsync (string calid, DateTime mindate, DateTime maxdate)
|
|
||||||
{
|
|
||||||
var service = new CalendarService();
|
|
||||||
|
|
||||||
var listRequest = service.Events.List(calid);
|
var listRequest = service.Events.List(calid);
|
||||||
return await listRequest.ExecuteAsync();
|
return await listRequest.ExecuteAsync();
|
||||||
}
|
}
|
||||||
public async Task<DateTimeChooserViewModel> CreateViewModelAsync(
|
public async Task<DateTimeChooserViewModel> CreateViewModelAsync(
|
||||||
string inputId,
|
string inputId,
|
||||||
string calid, DateTime mindate, DateTime maxdate)
|
string calid, DateTime mindate, DateTime maxdate)
|
||||||
{
|
{
|
||||||
if (calid ==null) return new DateTimeChooserViewModel {
|
if (calid == null)
|
||||||
InputId = inputId,
|
return new DateTimeChooserViewModel
|
||||||
MinDate = mindate,
|
{
|
||||||
MaxDate = maxdate
|
InputId = inputId,
|
||||||
};
|
MinDate = mindate,
|
||||||
|
MaxDate = maxdate
|
||||||
|
};
|
||||||
|
|
||||||
|
var eventList = await GetCalendarAsync(calid, mindate, maxdate);
|
||||||
|
List<Period> free = new List<Period>();
|
||||||
|
List<Period> busy = new List<Period>();
|
||||||
|
|
||||||
var eventList = await GetCalendarAsync(calid, mindate, maxdate);
|
|
||||||
List<Period> free = new List<Period> ();
|
|
||||||
List<Period> busy = new List<Period> ();
|
|
||||||
|
|
||||||
foreach (var ev in eventList.Items)
|
foreach (var ev in eventList.Items)
|
||||||
{
|
{
|
||||||
DateTime start = ev.Start.DateTime.Value;
|
DateTime start = ev.Start.DateTime.Value;
|
||||||
DateTime end = ev.End.DateTime.Value;
|
DateTime end = ev.End.DateTime.Value;
|
||||||
|
|
||||||
if (ev.Transparency == "transparent" )
|
if (ev.Transparency == "transparent")
|
||||||
{
|
{
|
||||||
|
|
||||||
free.Add(new Period { Start = start, End = end });
|
free.Add(new Period { Start = start, End = end });
|
||||||
}
|
}
|
||||||
else busy.Add(new Period { Start = start, End = end });
|
else busy.Add(new Period { Start = start, End = end });
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DateTimeChooserViewModel {
|
return new DateTimeChooserViewModel
|
||||||
InputId = inputId,
|
{
|
||||||
MinDate = mindate,
|
InputId = inputId,
|
||||||
MaxDate = maxdate,
|
MinDate = mindate,
|
||||||
Free = free.ToArray(),
|
MaxDate = maxdate,
|
||||||
Busy = busy.ToArray(),
|
Free = free.ToArray(),
|
||||||
FreeDates = free.SelectMany( p => new string [] { p.Start.ToString("DD/mm/yyyy"), p.End.ToString("DD/mm/yyyy") }).Distinct().ToArray(),
|
Busy = busy.ToArray(),
|
||||||
BusyDates = busy.SelectMany( p => new string [] { p.Start.ToString("DD/mm/yyyy"), p.End.ToString("DD/mm/yyyy") }).Distinct().ToArray()
|
FreeDates = free.SelectMany(p => new string[] { p.Start.ToString("DD/mm/yyyy"), p.End.ToString("DD/mm/yyyy") }).Distinct().ToArray(),
|
||||||
};
|
BusyDates = busy.SelectMany(p => new string[] { p.Start.ToString("DD/mm/yyyy"), p.End.ToString("DD/mm/yyyy") }).Distinct().ToArray()
|
||||||
}
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a event in a calendar
|
/// Creates a event in a calendar
|
||||||
/// <c>calendar.events.insert</c>
|
/// <c>calendar.events.insert</c>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="calid"></param>
|
/// <param name="calid"></param>
|
||||||
/// <param name="startDate"></param>
|
/// <param name="startDate"></param>
|
||||||
/// <param name="lengthInSeconds"></param>
|
/// <param name="lengthInSeconds"></param>
|
||||||
/// <param name="summary"></param>
|
/// <param name="summary"></param>
|
||||||
/// <param name="description"></param>
|
/// <param name="description"></param>
|
||||||
/// <param name="location"></param>
|
/// <param name="location"></param>
|
||||||
/// <param name="available"></param>
|
/// <param name="available"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<Event> CreateEventAsync(string calid, DateTime startDate, int lengthInSeconds, string summary, string description, string location, bool available)
|
public async Task<Event> CreateEventAsync(string calid, DateTime startDate, int lengthInSeconds, string summary, string description, string location, bool available)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace (calid))
|
if (string.IsNullOrWhiteSpace(calid))
|
||||||
throw new Exception ("the calendar identifier is not specified");
|
throw new Exception("the calendar identifier is not specified");
|
||||||
var creds = GetServiceAccountCredential();
|
GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
|
||||||
GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
|
var computeService = new BaseClientService.Initializer()
|
||||||
var computeService = new BaseClientService.Initializer()
|
{
|
||||||
{
|
HttpClientInitializer = credential
|
||||||
HttpClientInitializer = credential
|
};
|
||||||
};
|
computeService.ApiKey = Startup.GoogleSettings.ApiKey;
|
||||||
computeService.ApiKey = Startup.GoogleSettings.ApiKey;
|
computeService.ApplicationName = "Yavsc";
|
||||||
computeService.ApplicationName = "Yavsc";
|
computeService.Validate();
|
||||||
computeService.Validate();
|
|
||||||
|
|
||||||
var service = new CalendarService();
|
var service = new CalendarService();
|
||||||
Event ev = new Event {
|
Event ev = new Event
|
||||||
|
{
|
||||||
Start = new EventDateTime { DateTime = startDate },
|
Start = new EventDateTime { DateTime = startDate },
|
||||||
End = new EventDateTime { DateTime = startDate.AddSeconds(lengthInSeconds) },
|
End = new EventDateTime { DateTime = startDate.AddSeconds(lengthInSeconds) },
|
||||||
Summary = summary,
|
Summary = summary,
|
||||||
Description = description
|
Description = description
|
||||||
};
|
};
|
||||||
var insert = service.Events.Insert(ev,calid);
|
var insert = service.Events.Insert(ev, calid);
|
||||||
var inserted = await insert.ExecuteAsync();
|
var inserted = await insert.ExecuteAsync();
|
||||||
|
|
||||||
return inserted;
|
return inserted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<CalendarService> GetServiceAsync()
|
||||||
|
{
|
||||||
|
GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
|
||||||
|
return new CalendarService(new BaseClientService.Initializer()
|
||||||
|
{
|
||||||
|
HttpClientInitializer = credential,
|
||||||
|
ApplicationName = "Yavsc"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates Google User Credential
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">Yavsc use id</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<CalendarService> CreateUserCalendarServiceAsync(string userId)
|
||||||
|
{
|
||||||
|
var login = await _dbContext.GetGoogleUserLoginAsync(userId);
|
||||||
|
var token = await _flow.LoadTokenAsync(login.ProviderKey, CancellationToken.None);
|
||||||
|
UserCredential cred = new UserCredential(_flow,login.ProviderKey,token);
|
||||||
|
return new CalendarService(new BaseClientService.Initializer()
|
||||||
|
{
|
||||||
|
HttpClientInitializer = cred,
|
||||||
|
ApplicationName = "Yavsc"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ namespace Yavsc.Services
|
|||||||
/// I calendar manager.
|
/// I calendar manager.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ICalendarManager {
|
public interface ICalendarManager {
|
||||||
Task<CalendarList> GetCalendarsAsync (string userId);
|
Task<CalendarList> GetCalendarsAsync (string userId, string pageToken);
|
||||||
Task<Events> GetCalendarAsync (string calid, DateTime mindate, DateTime maxdate);
|
Task<Events> GetCalendarAsync (string calid, DateTime mindate, DateTime maxdate);
|
||||||
Task<DateTimeChooserViewModel> CreateViewModelAsync(
|
Task<DateTimeChooserViewModel> CreateViewModelAsync(
|
||||||
string inputId,
|
string inputId,
|
||||||
|
@ -15,9 +15,13 @@ using Microsoft.Extensions.OptionsModel;
|
|||||||
using Microsoft.Extensions.WebEncoders;
|
using Microsoft.Extensions.WebEncoders;
|
||||||
using OAuth.AspNet.AuthServer;
|
using OAuth.AspNet.AuthServer;
|
||||||
using OAuth.AspNet.Tokens;
|
using OAuth.AspNet.Tokens;
|
||||||
|
using Google.Apis.Util.Store;
|
||||||
using Yavsc.Auth;
|
using Yavsc.Auth;
|
||||||
using Yavsc.Extensions;
|
using Yavsc.Extensions;
|
||||||
using Yavsc.Models;
|
using Yavsc.Models;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Google.Apis.Auth.OAuth2.Responses;
|
||||||
|
|
||||||
namespace Yavsc
|
namespace Yavsc
|
||||||
{
|
{
|
||||||
@ -87,7 +91,7 @@ namespace Yavsc
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
private void ConfigureOAuthApp(IApplicationBuilder app,
|
private void ConfigureOAuthApp(IApplicationBuilder app,
|
||||||
SiteSettings settingsOptions)
|
SiteSettings settingsOptions, ILogger logger)
|
||||||
{
|
{
|
||||||
|
|
||||||
app.UseIdentity();
|
app.UseIdentity();
|
||||||
@ -144,9 +148,18 @@ namespace Yavsc
|
|||||||
{
|
{
|
||||||
var gcontext = context as GoogleOAuthCreatingTicketContext;
|
var gcontext = context as GoogleOAuthCreatingTicketContext;
|
||||||
context.Identity.AddClaim(new Claim(YavscClaimTypes.GoogleUserId, gcontext.GoogleUserId));
|
context.Identity.AddClaim(new Claim(YavscClaimTypes.GoogleUserId, gcontext.GoogleUserId));
|
||||||
var service =
|
var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
|
||||||
serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
|
await dbContext.StoreTokenAsync(gcontext.GoogleUserId, context.TokenResponse);
|
||||||
await service.StoreTokenAsync(gcontext.GoogleUserId, context.TokenResponse);
|
var store = serviceScope.ServiceProvider.GetService<IDataStore>();
|
||||||
|
|
||||||
|
await store.StoreAsync(gcontext.GoogleUserId, new TokenResponse {
|
||||||
|
AccessToken = gcontext.TokenResponse.AccessToken,
|
||||||
|
RefreshToken = gcontext.TokenResponse.RefreshToken,
|
||||||
|
TokenType = gcontext.TokenResponse.TokenType,
|
||||||
|
ExpiresInSeconds = int.Parse(gcontext.TokenResponse.ExpiresIn),
|
||||||
|
IssuedUtc = DateTime.Now
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ namespace Yavsc
|
|||||||
{
|
{
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using Formatters;
|
using Formatters;
|
||||||
|
using Google.Apis.Util.Store;
|
||||||
using Microsoft.Extensions.Localization;
|
using Microsoft.Extensions.Localization;
|
||||||
using Models;
|
using Models;
|
||||||
using PayPal.Manager;
|
using PayPal.Manager;
|
||||||
@ -230,6 +231,7 @@ namespace Yavsc
|
|||||||
services.AddTransient<IEmailSender, AuthMessageSender>();
|
services.AddTransient<IEmailSender, AuthMessageSender>();
|
||||||
services.AddTransient<IGoogleCloudMessageSender, AuthMessageSender>();
|
services.AddTransient<IGoogleCloudMessageSender, AuthMessageSender>();
|
||||||
services.AddTransient<IBillingService, BillingService>();
|
services.AddTransient<IBillingService, BillingService>();
|
||||||
|
services.AddTransient<IDataStore, FileDataStore>( (sp) => new FileDataStore("googledatastore",false) );
|
||||||
services.AddTransient<ICalendarManager, CalendarManager>();
|
services.AddTransient<ICalendarManager, CalendarManager>();
|
||||||
|
|
||||||
// TODO for SMS: services.AddTransient<ISmsSender, AuthMessageSender>();
|
// TODO for SMS: services.AddTransient<ISmsSender, AuthMessageSender>();
|
||||||
@ -347,7 +349,7 @@ namespace Yavsc
|
|||||||
options.AutomaticAuthentication = false;
|
options.AutomaticAuthentication = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
ConfigureOAuthApp(app, SiteSetup);
|
ConfigureOAuthApp(app, SiteSetup, logger);
|
||||||
ConfigureFileServerApp(app, SiteSetup, env, authorizationService);
|
ConfigureFileServerApp(app, SiteSetup, env, authorizationService);
|
||||||
ConfigureWebSocketsApp(app, SiteSetup, env);
|
ConfigureWebSocketsApp(app, SiteSetup, env);
|
||||||
ConfigureWorkflow(app, SiteSetup, logger);
|
ConfigureWorkflow(app, SiteSetup, logger);
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
using Google.Apis.Calendar.v3.Data;
|
||||||
|
|
||||||
namespace Yavsc.ViewModels.Calendar
|
namespace Yavsc.ViewModels.Calendar
|
||||||
{
|
{
|
||||||
public class SetGoogleCalendarViewModel
|
public class SetGoogleCalendarViewModel
|
||||||
@ -6,6 +8,8 @@ namespace Yavsc.ViewModels.Calendar
|
|||||||
public string GoogleCalendarId { get; set; }
|
public string GoogleCalendarId { get; set; }
|
||||||
|
|
||||||
public string ReturnUrl { get; set; }
|
public string ReturnUrl { get; set; }
|
||||||
|
|
||||||
|
public CalendarList Calendars { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,16 @@
|
|||||||
}
|
}
|
||||||
<form asp-action="SetGoogleCalendar">
|
<form asp-action="SetGoogleCalendar">
|
||||||
@{ var entryNum=0; }
|
@{ var entryNum=0; }
|
||||||
@foreach (var calendar in ViewBag.Calendars?.items)
|
@foreach (var calendar in Model.Calendars.Items)
|
||||||
{
|
{
|
||||||
entryNum++;
|
entryNum++;
|
||||||
@if (calendar.accessRole=="owner") {
|
@if (calendar.AccessRole=="owner") {
|
||||||
<label >
|
<label >
|
||||||
<input type="radio" name="GoogleCalendarId" value="@calendar.id" id="cal@entryNum">
|
<input type="radio" name="GoogleCalendarId" value="@calendar.Id" id="cal@entryNum">
|
||||||
|
|
||||||
<span style="background-color: @calendar.backgroundColor; foreground-color: @calendar.foregroundColor;">
|
<span style="background-color: @calendar.BackgroundColor; foreground-color: @calendar.ForegroundColor;">
|
||||||
@calendar.summary </span>
|
@calendar.Summary </span>
|
||||||
<i>"@calendar.description"</i>
|
<i>"@calendar.Description"</i>
|
||||||
</label>
|
</label>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -49,4 +49,3 @@
|
|||||||
@inject IOptions<GoogleAuthSettings> GoogleSettings
|
@inject IOptions<GoogleAuthSettings> GoogleSettings
|
||||||
@inject IOptions<SiteSettings> SiteSettings
|
@inject IOptions<SiteSettings> SiteSettings
|
||||||
@inject IHostingEnvironment HostingEnvironment
|
@inject IHostingEnvironment HostingEnvironment
|
||||||
@inject ICalendarManager CalendarManager;
|
|
||||||
|
@ -1241,7 +1241,6 @@
|
|||||||
<Content Include="GoogleApiSupport\Google.Apis\Requests\HttpRequestMessageExtenstions.cs" />
|
<Content Include="GoogleApiSupport\Google.Apis\Requests\HttpRequestMessageExtenstions.cs" />
|
||||||
<Content Include="GoogleApiSupport\Google.Apis.Core\Requests\RequestBuilder.cs" />
|
<Content Include="GoogleApiSupport\Google.Apis.Core\Requests\RequestBuilder.cs" />
|
||||||
<Content Include="GoogleApiSupport\Google.Apis.Core\Testing\VisibleForTestOnly.cs" />
|
<Content Include="GoogleApiSupport\Google.Apis.Core\Testing\VisibleForTestOnly.cs" />
|
||||||
<Content Include="Services\GoogleApis\CalendarClient.cs" />
|
|
||||||
<Content Include="GoogleApiSupport\Google.Apis.Core\Json\NewtonsoftJsonSerializer.cs" />
|
<Content Include="GoogleApiSupport\Google.Apis.Core\Json\NewtonsoftJsonSerializer.cs" />
|
||||||
<Content Include="GoogleApiSupport\Google.Apis.Core\Json\IJsonSerializer.cs" />
|
<Content Include="GoogleApiSupport\Google.Apis.Core\Json\IJsonSerializer.cs" />
|
||||||
<Content Include="GoogleApiSupport\Google.Apis.Core\Discovery\Features.cs" />
|
<Content Include="GoogleApiSupport\Google.Apis.Core\Discovery\Features.cs" />
|
||||||
@ -1249,6 +1248,13 @@
|
|||||||
<Content Include="GoogleApiSupport\Google.Apis.Core\Http\ConfigurableHttpClient.cs" />
|
<Content Include="GoogleApiSupport\Google.Apis.Core\Http\ConfigurableHttpClient.cs" />
|
||||||
<Content Include="GoogleApiSupport\Google.Apis.Core\Http\IHttpClientFactory.cs" />
|
<Content Include="GoogleApiSupport\Google.Apis.Core\Http\IHttpClientFactory.cs" />
|
||||||
<Content Include="Services\GoogleApis\Google.Apis.Calendar.v3.cs" />
|
<Content Include="Services\GoogleApis\Google.Apis.Calendar.v3.cs" />
|
||||||
|
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\Requests\RefreshTokenRequest.cs" />
|
||||||
|
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\AuthorizationCodeInstalledApp.cs" />
|
||||||
|
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\Responses\TokenResponse.cs" />
|
||||||
|
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\UserCredential.cs" />
|
||||||
|
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\Web\AuthWebUtility.cs" />
|
||||||
|
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\Web\AuthorizationCodeWebApp.cs" />
|
||||||
|
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\Flows\IAuthorizationCodeFlow.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="../Yavsc.Abstract/Yavsc.Abstract.csproj" />
|
<Import Project="../Yavsc.Abstract/Yavsc.Abstract.csproj" />
|
||||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
|
Reference in New Issue
Block a user