public static GoogleCalendarService GetServiceClient(GoogleClient config, string token) { var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = config.ClientId, ClientSecret = config.ClientSecret, }, Scopes = config.Scopes, DataStore = new FileDataStore("Store"), }); var tokenRes = new TokenResponse { AccessToken = token, ExpiresInSeconds = 3600, IssuedUtc = DateTime.UtcNow, }; var credential = new UserCredential(flow, Environment.UserName, tokenRes); var calendarService = new GoogleCalendarService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = config.ApplicationName, }); return(calendarService); }
/// <summary> /// Initializes a new instance of the <see cref="GoogleCalendarAPI"/> class. /// </summary> /// <param name="token">access token.</param> public GoogleCalendarAPI(string token) { var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = "21265915303-86gk1759nok7tqsa5k6ee27eepqfhdht.apps.googleusercontent.com", ClientSecret = "66dvHAEq_51fLhVPw6kg200R", }, Scopes = Scopes, DataStore = new FileDataStore("Store"), }); var tokenRes = new TokenResponse { AccessToken = token, ExpiresInSeconds = 3600, IssuedUtc = DateTime.UtcNow, }; var credential = new UserCredential(flow, Environment.UserName, tokenRes); service = new Google.Apis.Calendar.v3.CalendarService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = applicationName, }); }
public async Task <IEnumerable <Domain.Entities.Calendar.Event> > GetCalendarEvents(DateTime?minDate, DateTime?maxDate) { string credentials = Configuration.GetConfigurationFile("calendar_credentials"); if (!File.Exists(credentials)) { throw new UnconfiguredException("Please download the credentials. See the Readme."); } UserCredential credential = null; using (var stream = new FileStream(credentials, FileMode.Open, FileAccess.Read)) { string token = Configuration.GetConfigurationFile("calendar_token"); credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(token, true)).Result; } if (credential is null) { throw new UnauthorizedException("Failed to login to Google Calendar"); } // Create Google Calendar API service. var service = new GoogleCalendarService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); // Define parameters of request. EventsResource.ListRequest request = service.Events.List("primary"); var now = DateTime.Now; request.TimeMin = minDate; request.TimeMax = maxDate; request.ShowHiddenInvitations = false; request.ShowDeleted = false; request.SingleEvents = true; request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime; // List events. Events events = await request.ExecuteAsync(); return(events.Items.Select(e => new Domain.Entities.Calendar.Event { Start = e.Start.DateTime, End = e.End.DateTime, Summary = e.Summary })); }
private Google.Apis.Calendar.v3.CalendarService getService() { try { // Create Google Calendar API service. var credential = getCredentials(); var service = new Google.Apis.Calendar.v3.CalendarService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Calendar" }); return(service); } catch (Exception) { return(null); } }
/// <summary> /// Initializes a new instance of the <see cref="GoogleCalendarAPI"/> class. /// </summary> /// <param name="googleCalendarService">GoogleClient. </param> public GoogleCalendarAPI(GoogleCalendarService googleCalendarService) { _service = googleCalendarService; }
public bool SaveToGmail() { var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description); provider.ClientIdentifier = "953955382732.apps.googleusercontent.com"; provider.ClientSecret = "mYUQelU9LUiozb-0Qw6l0rBK"; var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization); var srv = new Google.Apis.Calendar.v3.CalendarService(auth); Calendar calendar = ClearGMail(srv); if (calendar == null) { return false; } foreach (Event e in events) { srv.Events.Insert(e, calendar.Id).Fetch(); } return true; }