Exemple #1
0
        public void Init()
        {
            //string timestamp = DateTime.Now.Ticks.ToString();
            if (_logUpdateHandler == null)
            {
                _logUpdateHandler  = new Logger.LogUpdatedHandler(Logger_LogUpdated);
                Logger.LogUpdated += _logUpdateHandler;
            }

            string gmailUsername;
            //string gmailPassword;
            string syncProfile;
            string syncContactsFolder;
            string syncNotesFolder;
            string syncAppointmentsFolder;

            GoogleAPITests.LoadSettings(out gmailUsername, out syncProfile, out syncContactsFolder, out syncNotesFolder, out syncAppointmentsFolder);

            sync = new Synchronizer();
            sync.SyncContacts            = false;
            sync.SyncNotes               = true;
            sync.SyncAppointments        = false;
            sync.SyncProfile             = syncProfile;
            Synchronizer.SyncNotesFolder = syncNotesFolder;

            sync.LoginToGoogle(gmailUsername);
            sync.LoginToOutlook();
        }
        public void CreateNewContact()
        {
            string gmailUsername;
            string syncProfile;

            GoogleAPITests.LoadSettings(out gmailUsername, out syncProfile);

            ContactsRequest service;

            var scopes = new List <string>();

            //Contacts-Scope
            scopes.Add("https://www.google.com/m8/feeds");
            //Notes-Scope
            scopes.Add("https://docs.google.com/feeds/");
            //scopes.Add("https://docs.googleusercontent.com/");
            //scopes.Add("https://spreadsheets.google.com/feeds/");
            //Calendar-Scope
            //scopes.Add("https://www.googleapis.com/auth/calendar");
            scopes.Add(CalendarService.Scope.Calendar);

            UserCredential credential;

            byte[] jsonSecrets = Properties.Resources.client_secrets;

            using (var stream = new MemoryStream(jsonSecrets))
            {
                FileDataStore fDS = new FileDataStore(Logger.AuthFolder, true);

                GoogleClientSecrets clientSecrets = GoogleClientSecrets.Load(stream);

                credential = GCSMOAuth2WebAuthorizationBroker.AuthorizeAsync(
                    clientSecrets.Secrets,
                    scopes.ToArray(),
                    gmailUsername,
                    CancellationToken.None,
                    fDS).
                             Result;

                OAuth2Parameters parameters = new OAuth2Parameters
                {
                    ClientId     = clientSecrets.Secrets.ClientId,
                    ClientSecret = clientSecrets.Secrets.ClientSecret,

                    // Note: AccessToken is valid only for 60 minutes
                    AccessToken  = credential.Token.AccessToken,
                    RefreshToken = credential.Token.RefreshToken
                };

                RequestSettings settings = new RequestSettings("GoContactSyncMod", parameters);

                service = new ContactsRequest(settings);
            }



            #region Delete previously created test contact.
            ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
            query.NumberToRetrieve = 500;

            Feed <Contact> feed = service.Get <Contact>(query);

            Logger.Log("Loaded Google contacts", EventType.Information);

            foreach (Contact entry in feed.Entries)
            {
                if (entry.PrimaryEmail != null && entry.PrimaryEmail.Address == "*****@*****.**")
                {
                    service.Delete(entry);
                    Logger.Log("Deleted Google contact", EventType.Information);
                    //break;
                }
            }
            #endregion

            Contact newEntry = new Contact();
            newEntry.Title = "John Doe";

            EMail primaryEmail = new EMail("*****@*****.**");
            primaryEmail.Primary = true;
            primaryEmail.Rel     = ContactsRelationships.IsWork;
            newEntry.Emails.Add(primaryEmail);

            PhoneNumber phoneNumber = new PhoneNumber("555-555-5551");
            phoneNumber.Primary = true;
            phoneNumber.Rel     = ContactsRelationships.IsMobile;
            newEntry.Phonenumbers.Add(phoneNumber);

            StructuredPostalAddress postalAddress = new StructuredPostalAddress();
            postalAddress.Street  = "123 somewhere lane";
            postalAddress.Primary = true;
            postalAddress.Rel     = ContactsRelationships.IsHome;
            newEntry.PostalAddresses.Add(postalAddress);

            newEntry.Content = "Who is this guy?";

            Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default"));

            Contact createdEntry = service.Insert(feedUri, newEntry);

            Logger.Log("Created Google contact", EventType.Information);

            Assert.IsNotNull(createdEntry.ContactEntry.Id.Uri);

            Contact updatedEntry = service.Update(createdEntry);

            Logger.Log("Updated Google contact", EventType.Information);

            //delete test contacts
            service.Delete(createdEntry);

            Logger.Log("Deleted Google contact", EventType.Information);
        }
        public void CreateNewNote()
        {
            string gmailUsername;
            string syncProfile;

            GoogleAPITests.LoadSettings(out gmailUsername, out syncProfile);

            DocumentsRequest service;

            var scopes = new List <string>();

            //Contacts-Scope
            scopes.Add("https://www.google.com/m8/feeds");
            //Notes-Scope
            scopes.Add("https://docs.google.com/feeds/");
            //scopes.Add("https://docs.googleusercontent.com/");
            //scopes.Add("https://spreadsheets.google.com/feeds/");
            //Calendar-Scope
            //scopes.Add("https://www.googleapis.com/auth/calendar");
            scopes.Add(CalendarService.Scope.Calendar);

            UserCredential credential;

            byte[] jsonSecrets = Properties.Resources.client_secrets;

            using (var stream = new MemoryStream(jsonSecrets))
            {
                FileDataStore fDS = new FileDataStore(Logger.AuthFolder, true);

                GoogleClientSecrets clientSecrets = GoogleClientSecrets.Load(stream);

                credential = GCSMOAuth2WebAuthorizationBroker.AuthorizeAsync(
                    clientSecrets.Secrets,
                    scopes.ToArray(),
                    gmailUsername,
                    CancellationToken.None,
                    fDS).
                             Result;

                OAuth2Parameters parameters = new OAuth2Parameters
                {
                    ClientId     = clientSecrets.Secrets.ClientId,
                    ClientSecret = clientSecrets.Secrets.ClientSecret,

                    // Note: AccessToken is valid only for 60 minutes
                    AccessToken  = credential.Token.AccessToken,
                    RefreshToken = credential.Token.RefreshToken
                };

                RequestSettings settings = new RequestSettings("GoContactSyncMod", parameters);

                service = new DocumentsRequest(settings);

                //Instantiate an Authenticator object according to your authentication, to use ResumableUploader
                _authenticator = new OAuth2Authenticator("GCSM Unit Tests", parameters);
            }



            //Delete previously created test note.
            DeleteTestNote(service);

            Document newEntry = new Document();

            newEntry.Type  = Document.DocumentType.Document;
            newEntry.Title = "AN_OUTLOOK_TEST_NOTE";

            string file = NotePropertiesUtils.CreateNoteFile("AN_OUTLOOK_TEST_NOTE", "This is just a test note to test GoContactSyncMod", null);

            newEntry.MediaSource = new MediaFileSource(file, MediaFileSource.GetContentTypeForFileName(file));

            #region normal flow, only working to create documents without content (metadata only), e.g. for Notes folder

            Document createdEntry = Synchronizer.SaveGoogleNote(null, newEntry, service);

            Assert.IsNotNull(createdEntry.DocumentEntry.Id.Uri);

            Logger.Log("Created Google note", EventType.Information);

            //Wait 5 seconds to give the testcase the chance to finish
            System.Threading.Thread.Sleep(5000);

            //delete test note
            DeleteTestNote(service);
            #endregion

            #region workaround flow to use UploadDocument, not needed anymore because of new approach to use ResumableUploader
            //Google.GData.Documents.DocumentEntry createdEntry2 = service.Service.UploadDocument(file, newEntry.Title);

            //Assert.IsNotNull(createdEntry2.Id.Uri);

            ////delete test note
            //DeleteTestNote(service);
            #endregion

            #region New approach how to update an existing document: https://developers.google.com/google-apps/documents-list/#updatingchanging_documents_and_files
            //Instantiate the ResumableUploader component.
            ResumableUploader uploader = new ResumableUploader();
            uploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(OnGoogleNoteCreated);
            Synchronizer.CreateGoogleNote(newEntry, file, service, uploader, _authenticator);
            #endregion

            //Wait 5 seconds to give the testcase the chance to finish the Async events
            System.Threading.Thread.Sleep(5000);

            DeleteTestNote(service);
        }
        public void CreateNewAppointment()
        {
            string gmailUsername;
            string syncProfile;

            GoogleAPITests.LoadSettings(out gmailUsername, out syncProfile);

            EventsResource    service;
            CalendarListEntry primaryCalendar = null;
            var scopes = new List <string>();

            //Contacts-Scope
            scopes.Add("https://www.google.com/m8/feeds");
            //Notes-Scope
            scopes.Add("https://docs.google.com/feeds/");
            //scopes.Add("https://docs.googleusercontent.com/");
            //scopes.Add("https://spreadsheets.google.com/feeds/");
            //Calendar-Scope
            //scopes.Add("https://www.googleapis.com/auth/calendar");
            scopes.Add(CalendarService.Scope.Calendar);

            UserCredential credential;

            byte[] jsonSecrets = Properties.Resources.client_secrets;

            //using (var stream = new FileStream(Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(this.GetType()).Location) + "\\client_secrets.json", FileMode.Open, FileAccess.Read))
            //using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            //using (var stream = new FileStream(Application.StartupPath + "\\client_secrets.json", FileMode.Open, FileAccess.Read))
            using (var stream = new MemoryStream(jsonSecrets))
            {
                FileDataStore fDS = new FileDataStore(Logger.AuthFolder, true);
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets, scopes, gmailUsername, CancellationToken.None,
                    fDS).Result;

                var initializer = new Google.Apis.Services.BaseClientService.Initializer();
                initializer.HttpClientInitializer = credential;
                var CalendarRequest = new CalendarService(initializer);
                //CalendarRequest.setUserCredentials(username, password);

                var list = CalendarRequest.CalendarList.List().Execute().Items;
                foreach (var calendar in list)
                {
                    if (calendar.Primary != null && calendar.Primary.Value)
                    {
                        primaryCalendar = calendar;
                        break;
                    }
                }

                if (primaryCalendar == null)
                {
                    throw new Exception("Primary Calendar not found");
                }


                //EventQuery query = new EventQuery("https://www.google.com/calendar/feeds/default/private/full");
                //ToDo: Upgrade to v3, EventQuery query = new EventQuery("https://www.googleapis.com/calendar/v3/calendars/default/events");
                service = CalendarRequest.Events;
            }

            #region Delete previously created test contact.
            var query = service.List(primaryCalendar.Id);
            query.MaxResults = 500;
            query.TimeMin    = DateTime.Now.AddDays(-10);
            query.TimeMax    = DateTime.Now.AddDays(10);
            //query.Q = "GCSM Test Appointment";

            var feed = query.Execute();
            Logger.Log("Loaded Google appointments", EventType.Information);
            foreach (Google.Apis.Calendar.v3.Data.Event entry in feed.Items)
            {
                if (entry.Summary != null && entry.Summary.Contains("GCSM Test Appointment") && !entry.Status.Equals("cancelled"))
                {
                    Logger.Log("Deleting Google appointment:" + entry.Summary + " - " + entry.Start.DateTime.ToString(), EventType.Information);
                    service.Delete(primaryCalendar.Id, entry.Id);
                    Logger.Log("Deleted Google appointment", EventType.Information);
                    //break;
                }
            }



            #endregion

            var newEntry = Factory.NewEvent();
            newEntry.Summary        = "GCSM Test Appointment";
            newEntry.Start.DateTime = DateTime.Now;
            newEntry.End.DateTime   = DateTime.Now;

            var createdEntry = service.Insert(newEntry, primaryCalendar.Id).Execute();

            Logger.Log("Created Google appointment", EventType.Information);

            Assert.IsNotNull(createdEntry.Id);

            var updatedEntry = service.Update(createdEntry, primaryCalendar.Id, createdEntry.Id).Execute();

            Logger.Log("Updated Google appointment", EventType.Information);

            //delete test contacts
            service.Delete(primaryCalendar.Id, updatedEntry.Id).Execute();

            Logger.Log("Deleted Google appointment", EventType.Information);
        }