public void CreateNewContact() { try { ContactsService service = new ContactsService("WebGear.GoogleContactsSync"); service.setUserCredentials(ConfigurationManager.AppSettings["Gmail.Username"], ConfigurationManager.AppSettings["Gmail.Password"]); #region Delete previously created test contact. ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default")); query.NumberToRetrieve = 500; ContactsFeed feed = service.Query(query); foreach (ContactEntry entry in feed.Entries) { if (entry.PrimaryEmail != null && entry.PrimaryEmail.Address == "*****@*****.**") { entry.Delete(); break; } } #endregion ContactEntry newEntry = new ContactEntry(); newEntry.Title.Text = "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); PostalAddress postalAddress = new PostalAddress(); postalAddress.Value = "123 somewhere lane"; postalAddress.Primary = true; postalAddress.Rel = ContactsRelationships.IsHome; newEntry.PostalAddresses.Add(postalAddress); newEntry.Content.Content = "Who is this guy?"; Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default")); ContactEntry createdEntry = (ContactEntry)service.Insert(feedUri, newEntry); Assert.IsNotNull(createdEntry.Id.Uri); //delete this temp contact createdEntry.Delete(); } catch (Exception ex) { } }
////////////////////////////////////////////////////////////////////// /// <summary>runs an authentication test, inserts a new contact</summary> ////////////////////////////////////////////////////////////////////// [Test] public void InsertContactsTest() { const int numberOfInserts = 37; Tracing.TraceMsg("Entering InsertContactsTest"); ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri(this.userName + "@googlemail.com")); ContactsService service = new ContactsService("unittests"); if (this.userName != null) { service.Credentials = new GDataCredentials(this.userName, this.passWord); } ContactsFeed feed = service.Query(query); int originalCount = feed.Entries.Count; PhoneNumber p = null; List<ContactEntry> inserted = new List<ContactEntry>(); if (feed != null) { Assert.IsTrue(feed.Entries != null, "the contacts needs entries"); for (int i = 0; i < numberOfInserts; i++) { ContactEntry entry = ObjectModelHelper.CreateContactEntry(i); entry.PrimaryEmail.Address = "joe" + i.ToString() + "@doe.com"; p = entry.PrimaryPhonenumber; inserted.Add(feed.Insert(entry)); } } List<ContactEntry> list = new List<ContactEntry>(); feed = service.Query(query); foreach (ContactEntry e in feed.Entries) { list.Add(e); } while (feed.NextChunk != null) { ContactsQuery nq = new ContactsQuery(feed.NextChunk); feed = service.Query(nq); foreach (ContactEntry e in feed.Entries) { list.Add(e); } } if (inserted.Count > 0) { int iVer = numberOfInserts; // let's find those guys for (int i = 0; i < inserted.Count; i++) { ContactEntry test = inserted[i] as ContactEntry; foreach (ContactEntry e in list) { if (e.Id == test.Id) { iVer--; // verify we got the phonenumber back.... Assert.IsTrue(e.PrimaryPhonenumber != null, "They should have a primary phonenumber"); Assert.AreEqual(e.PrimaryPhonenumber.Value,p.Value, "They should be identical"); } } } Assert.IsTrue(iVer == 0, "The new entries should all be part of the feed now, we have " + iVer + " now"); } // now delete them again foreach (ContactEntry e in inserted) { e.Delete(); } // now make sure they are gone if (inserted.Count > 0) { feed = service.Query(query); // let's find those guys, we should not find ANY for (int i = 0; i < inserted.Count; i++) { ContactEntry test = inserted[i] as ContactEntry; foreach (ContactEntry e in feed.Entries) { Assert.IsTrue(e.Id != test.Id, "The new entries should all be deleted now"); } } Assert.IsTrue(feed.Entries.Count == originalCount, "The count should be correct as well"); } }
///////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// /// <summary>runs an authentication test</summary> ////////////////////////////////////////////////////////////////////// [Test] public void ContactsAuthenticationTest() { Tracing.TraceMsg("Entering ContactsAuthenticationTest"); ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri(this.userName + "@googlemail.com")); ContactsService service = new ContactsService("unittests"); if (this.userName != null) { service.Credentials = new GDataCredentials(this.userName, this.passWord); } ContactsFeed feed = service.Query(query); ObjectModelHelper.DumpAtomObject(feed,CreateDumpFileName("ContactsAuthTest")); if (feed != null && feed.Entries.Count > 0) { Tracing.TraceMsg("Found a Feed " + feed.ToString()); foreach (ContactEntry entry in feed.Entries) { Assert.IsTrue(entry.Etag != null, "contact entries should have etags"); } } }
///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// /// <summary>runs an basic auth test against the groups feed test</summary> ////////////////////////////////////////////////////////////////////// [Test] public void GroupsSystemTest() { Tracing.TraceMsg("Entering GroupsSystemTest"); GroupsQuery query = new GroupsQuery(ContactsQuery.CreateGroupsUri(this.userName + "@googlemail.com")); ContactsService service = new ContactsService("unittests"); if (this.userName != null) { service.Credentials = new GDataCredentials(this.userName, this.passWord); } GroupsFeed feed = service.Query(query); int i = 0; foreach (GroupEntry g in feed.Entries ) { if (g.SystemGroup != null) { i++; } } Assert.IsTrue(i==4, "There should be 4 system groups in the groups feed"); ObjectModelHelper.DumpAtomObject(feed,CreateDumpFileName("GroupsAuthTest")); GroupEntry newGroup = new GroupEntry(); newGroup.Title.Text = "Private Data"; GroupEntry insertedGroup = feed.Insert(newGroup); GroupEntry g2 = new GroupEntry(); g2.Title.Text = "Another Private Group"; GroupEntry insertedGroup2 = feed.Insert(g2); // now insert a new contact that belongs to that group ContactsQuery q = new ContactsQuery(ContactsQuery.CreateContactsUri(this.userName + "@googlemail.com")); ContactsFeed cf = service.Query(q); ContactEntry entry = ObjectModelHelper.CreateContactEntry(1); GroupMembership member = new GroupMembership(); member.HRef = insertedGroup.Id.Uri.ToString(); GroupMembership member2 = new GroupMembership(); member2.HRef = insertedGroup2.Id.Uri.ToString(); ContactEntry insertedEntry = cf.Insert(entry); // now change the group membership insertedEntry.GroupMembership.Add(member); insertedEntry.GroupMembership.Add(member2); ContactEntry currentEntry = insertedEntry.Update(); Assert.IsTrue(currentEntry.GroupMembership.Count == 2, "The entry should be in 2 groups"); currentEntry.GroupMembership.Clear(); currentEntry = currentEntry.Update(); // now we should have 2 new groups and one new entry with no groups anymore int oldCountGroups = feed.Entries.Count; int oldCountContacts = cf.Entries.Count; currentEntry.Delete(); insertedGroup.Delete(); insertedGroup2.Delete(); feed = service.Query(query); cf = service.Query(q); Assert.AreEqual(oldCountContacts, cf.Entries.Count, "Contacts count should be the same"); Assert.AreEqual(oldCountGroups, feed.Entries.Count, "Groups count should be the same"); }
public void ConflictContactsTest() { const int numberOfInserts = 50; const int numberWithAdds = 60; Tracing.TraceMsg("Entering InsertContactsTest"); ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri(this.userName + "@googlemail.com")); ContactsService service = new ContactsService("unittests"); if (this.userName != null) { service.Credentials = new GDataCredentials(this.userName, this.passWord); } // clean the contacts feed DeleteAllContacts(); ContactsFeed feed = service.Query(query); int originalCount = feed.Entries.Count; string email = Guid.NewGuid().ToString(); List<ContactEntry> inserted = new List<ContactEntry>(); // insert a number of guys for (int i = 0; i < numberOfInserts; i++) { ContactEntry entry = ObjectModelHelper.CreateContactEntry(i); entry.PrimaryEmail.Address = email + i.ToString() + "@doe.com"; entry = feed.Insert(entry); AddContactPhoto(entry, service); inserted.Add(entry); } if (feed != null) { for (int x = numberOfInserts; x <= numberWithAdds; x++) { for (int i = 0; i < x; i++) { ContactEntry entry = ObjectModelHelper.CreateContactEntry(i); entry.PrimaryEmail.Address = email + i.ToString() + "@doe.com"; try { entry = feed.Insert(entry); AddContactPhoto(entry, service); inserted.Add(entry); } catch (GDataRequestException) { } } } } List<ContactEntry> list = new List<ContactEntry>(); feed = service.Query(query); foreach (ContactEntry e in feed.Entries) { list.Add(e); } while (feed.NextChunk != null) { ContactsQuery nq = new ContactsQuery(feed.NextChunk); feed = service.Query(nq); foreach (ContactEntry e in feed.Entries) { list.Add(e); } } Assert.AreEqual(list.Count, numberWithAdds - originalCount, "We should have added new entries"); // clean the contacts feed DeleteAllContacts(); }
protected void ExecuteImport() { //start sync ContactsService GContactService = new ContactsService("Contact Infomation"); GContactService.setUserCredentials(email, password); ContactsQuery query = new ContactsQuery(ContactsQuery. CreateContactsUri("default")); ContactsFeed feed = null; try { feed = GContactService.Query(query); } catch (Exception) { this.setLabelText("Invalid email or password", Color.Red); return; } //start this.showProgressBar(feed.TotalResults); this.setLabelText("Importing...", Color.Black); int progress = 0; int startIndex = 0; while (feed.Entries.Count > 0) { startIndex += feed.ItemsPerPage; query.StartIndex = startIndex; PhoneNumbers.PhoneNumberUtil util = PhoneNumbers.PhoneNumberUtil.GetInstance(); foreach (ContactEntry entry in feed.Entries) { this.setProgress(progress); progress++; if (entry.Phonenumbers.Count > 0) { foreach (PhoneNumber number in entry.Phonenumbers) { string numb = string.Empty; try { PhoneNumbers.PhoneNumber num = util.Parse(number.Value, "NL"); numb = num.CountryCode.ToString() + num.NationalNumber.ToString(); } catch (Exception ex) { Console.WriteLine("Exception was thrown: " + ex.Message); continue; } if (!ContactStore.numberExists(numb + "@s.whatsapp.net")) { Contact contact = new Contact(0, numb + "@s.whatsapp.net", "", "", entry.Name.GivenName, entry.Name.FamilyName); ContactStore.AddContact(contact); } } } } feed = GContactService.Query(query); } //done! this.doExit(); }
private static ContactsFeed GetContactsFeed(ContactsQuery query, ContactsService service) { ContactsFeed feed = service.Query(query); return feed; }
private ActionResult OAuthCallback() { IAuthorizationState auth = client.ProcessUserAuthorization(Request); Session["auth"] = auth; var authFactory = new GAuthSubRequestFactory("cp", "Geeks Dilemma") {Token = auth.AccessToken}; var service = new ContactsService(authFactory.ApplicationName) {RequestFactory = authFactory}; var query = new ContactsQuery(ContactsQuery.CreateContactsUri("default")); query.NumberToRetrieve = 1000; ContactsFeed contacts = service.Query(query); ViewBag.ImportFrom = "Google"; var userId = GetCurrentUserId(); DeletePreviousImportIfNecessary(userId); var googleContact = new GoogleContact { UserId = userId, Contacts = (from ContactEntry entry in contacts.Entries from email in entry.Emails where entry.Name != null where email != null select new ImportModel { Import = false, EmailAddress = email.Address.ToLower(), Name = entry.Name.FullName }).ToList() }; RavenSession.Store(googleContact); return View("Import"); }
public void FetchTask() { if (syncEngine.SyncCanceled) { this.FetchSem.Release(); return; } mans.Clear(); contactsByFullName.Clear(); googleService = new ContactsService("Contact Infomation"); if (Credentials == null) { this.FetchSem.Release(); return; } else { if (Credentials.UserName != null && !Credentials.UserName.ToLower().EndsWith("@gmail.com")) { Credentials.UserName += "@gmail.com"; } googleService.setUserCredentials(Credentials.UserName, Credentials.Password); } ((GDataRequestFactory)googleService.RequestFactory).Timeout = 3000; queruUriFull = ContactsQuery.CreateContactsUri(Credentials.UserName, ContactsQuery.fullProjection); contactsQuery = new ContactsQuery(queruUriFull); contactsQuery.NumberToRetrieve = 1000; try { feed = googleService.Query(contactsQuery); } catch (Google.GData.Client.GDataRequestException e) { Credentials = null; Dispatcher.Invoke(new SyncCancelEventHandler(owner.CancelSync), this, e.InnerException.Message, null); this.FetchSem.Release(); return; } catch (Google.GData.Client.InvalidCredentialsException e) { Credentials = null; Dispatcher.Invoke(new SyncCancelEventHandler(owner.CancelSync), this, e.Message, null); this.FetchSem.Release(); return; } catch (CaptchaRequiredException e) { Credentials = null; Dispatcher.Invoke(new SyncCancelEventHandler(owner.CancelSync), this, e.Message, "https://www.google.com/accounts/UnlockCaptcha"); this.FetchSem.Release(); return; } catch (Exception e) { Credentials = null; Dispatcher.Invoke(new SyncCancelEventHandler(owner.CancelSync), this, e.Message, null); this.FetchSem.Release(); return; } syncEngine.CurrentTotal += feed.Entries.Count; GroupsQuery groupsQuery = new GroupsQuery(GroupsQuery. CreateGroupsUri(Credentials.UserName, ContactsQuery.thinProjection)); try { groupsFeed = googleService.Query(groupsQuery); } catch (Google.GData.Client.GDataRequestException e) { Credentials = null; Dispatcher.Invoke(new SyncCancelEventHandler(owner.CancelSync), this, e.InnerException.Message, null); this.FetchSem.Release(); return; } foreach (ContactEntry entry in feed.Entries) { if (owner != null) { Dispatcher.Invoke(new SyncProgressEventHandler(owner.Progress), this, "Loading " + "(" + syncEngine.CurrentItemNum + "/" + syncEngine.CurrentTotal + ")", syncEngine.CurrentItemNum, syncEngine.CurrentTotal); syncEngine.CurrentItemNum++; } Contact contact = GetCanonicalContact(entry); string fullname = contact.FullName; if (fullname == null || fullname.Trim() == "") { } else { contactsByFullName[fullname] = contact; mans.Add(new Man { FullName = fullname, EMail = contact.EMail, Phone = contact.Phone }); } } this.FetchSem.Release(); }
public string GoogleContactLookup(string username, string password, string lookup) { try { Log("Starting Google Contact Lookup for " + lookup + "."); ContactsService service = new ContactsService("sipsorcery-lookup"); ((GDataRequestFactory)service.RequestFactory).KeepAlive = false; service.setUserCredentials(username, password); var result = service.QueryClientLoginToken(); var query = new ContactsQuery(ContactsQuery.CreateContactsUri("default")); query.ExtraParameters = "q=" + lookup + "&max-results=1"; ContactsFeed feed = service.Query(query); if (feed != null && feed.Entries != null && feed.Entries.Count > 0) { var entry = feed.Entries.First() as ContactEntry; if (entry.Name != null && entry.Name.FullName.NotNullOrBlank()) { Log("Result found Google Contact Lookup for " + lookup + " of " + entry.Name.FullName + "."); return entry.Name.FullName; } else { Log("A result was found Google Contact Lookup for " + lookup + " but the FullName field was empty."); return null; } } else { Log("No result was found with a Google Contact Lookup for " + lookup + "."); return null; } } catch (Exception excp) { Log("Exception in GoogleContactLookup. " + excp.Message); return null; } }