mi problema es que yo segui la guia de https://developers.google.com/calendar/quickstart/dotnet (Sección .NET) y depurando del visual studio me funciona perfecto. pero cuando lo hosteaba en mi servidor en IIS. dejaba de funcionar. Googleando llegue a ver que muchos de los que tenian mi problema usaban otro tipo de autenticacion como la siguiente:
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "PUT_CLIENT_ID_HERE",
ClientSecret = "PUT_CLIENT_SECRET_HERE"
},
Scopes = new[] { DriveService.Scope.Drive },
DataStore = new FileDataStore("Drive.Api.Auth.Store")
});
como no pude implementar esta solucion mi codigo sigue igual que al principio.
[Services(ServicesExecutionMode.Command)]
public void SaveAuthorization(AuthorizationDto authorizationDto)
{
var entity = this._vacationRepository.GetById(authorizationDto.EntityId.Value);
entity.Authorized = authorizationDto.Authorize;
entity.AuthorizedDate = DateTime.Now;
entity.AuthorizerId = CurrentId;
this._vacationRepository.Persist(entity);
string auth = "rechazada";
if (authorizationDto.Authorize)
{
InsertCalendarEvent(entity);
auth = "aceptada";
}
Account account = _accountRepository.GetById(entity.AccountId);
string days = "desde el día " + entity.DateFrom.ToShortDateString() + " hasta el día " + entity.DateTo.ToShortDateString();
if (entity.DateFrom == entity.DateTo)
days = "el día " + entity.DateFrom.ToShortDateString();
_mailService.SendMail("Tu solicitud de vacaciones fue " + auth, account.Name + " tu solicitud de vacaciones " + days + " fue " + auth + ".", account.Mail);
}
Metodo que inserta el calendario:
private void InsertCalendarEvent(Vacation vacation)
{
string[] Scopes = { CalendarService.Scope.Calendar };
string ApplicationName = "Google Calendar API .NET Quickstart";
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var ev = new Event();
EventDateTime start = new EventDateTime();
start.DateTime = vacation.DateFrom;
EventDateTime end = new EventDateTime();
end.DateTime = vacation.DateTo;
ev.Start = start;
ev.End = end;
ev.Summary = vacation.Account.GetFullName() + " - Vacación";
ev.Description = "Días consumidos: " + vacation.ConsumedDays.ToString();
var calendarId = "primary";
Event recurringEvent = service.Events.Insert(ev, calendarId).Execute();
}
}
Intente ver algunos ejemplo de como implementar esto pero resulta que tampoco me sirvió porque es para projectos asp.net y el mio es .Net core. y no logre encontrar nadie que me muestre un ejemplo de como implementarlo para mi proyecto. Lo único que necesito es poder insertar eventos en un calendario en mi proyecto. Por favor si alguien me puede enviar un ejemplo me seria de gran ayuda. Gracias! Encontre una persona que tiene el mismo problema quizas se explica mejor que yo: https://stackoverflow.com/questions/44842813/google-calendar-api-working-fine-locally-but-not-raising-its-authentication-on-s-s)