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)
            {

            }
        }
Exemple #2
0
            protected override void ProcessRecord()
            {
                adminUser = credentials.UserName;
                adminPassword = new Dgc.ConvertToUnsecureString(credentials.Password).PlainString;
                var _domain = dgcGoogleAppsService.GetDomain(adminUser);

                try
                {
                    //AppsService
                    service.AppsService = new AppsService(_domain, adminUser, adminPassword);

                    //CalendarService
                    var _calendarService = new CalendarService("Calendar");
                    _calendarService.setUserCredentials(adminUser, adminPassword);
                    service.CalendarService = _calendarService;

                    //OauthCalendarService
                    if (consumerKey != null)
                    {
                        if (consumerSecret == null)
                        {
                            throw new Exception("-ConsumerSecret can't be null");
                        }
                        var _oauthCalendarService = new CalendarService("Calendar");
                        var _oauth = new GDataTypes.Oauth();
                        _oauth.ConsumerKey = consumerKey;
                        _oauth.ConsumerSecret = consumerSecret;
                        service.Oauth = _oauth;
                        GOAuthRequestFactory _requestFactory = new GOAuthRequestFactory("cl", "GDataCmdLet");
                        _requestFactory.ConsumerKey = _oauth.ConsumerKey;
                        _requestFactory.ConsumerSecret = _oauth.ConsumerSecret;
                        _oauthCalendarService.RequestFactory = _requestFactory;
                        service.OauthCalendarService = _oauthCalendarService;
                    }

                    //MailSettingsService
                    var _googleMailSettingsService = new GoogleMailSettingsService(_domain, "GMailSettingsService");
                    _googleMailSettingsService.setUserCredentials(adminUser, adminPassword);
                    service.GoogleMailSettingsService = _googleMailSettingsService;

                    //ProfileService
                    var _dgcGoogleProfileService = new Dgc.GoogleProfileService();
                    service.ProfileService = _dgcGoogleProfileService.GetAuthToken(adminUser, adminPassword);

                    //ResourceService
                    var _dgcGoogleResourceService = new Dgc.GoogleResourceService();
                    service.ResourceService = _dgcGoogleResourceService.GetAuthToken(adminUser, adminPassword);

                    //ContactsService
                    var _contactService = new ContactsService("GData");
                    _contactService.setUserCredentials(adminUser, adminPassword);
                    service.ContactsService = _contactService;

                    WriteObject(service);
                }
                catch (AppsException _exception)
                {
                    WriteObject(_exception, true);
                }
            }
        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 ContactsService CreateService(string username, string password, string appName)
 {
     ContactsService service = new ContactsService(appName);
     service.setUserCredentials(username, password);
     return service;
 }
		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();
		}
        private void GetContactList(string username, string password)
        {
            contactTable.Columns.Add("Photo", typeof(System.Drawing.Bitmap));
            contactTable.Columns.Add("Name", typeof(string));
            contactTable.Columns.Add("Email", typeof(string));
            toDataGrid.DataSource = contactTable;
            toDataGrid.ShowCellToolTips = false;
            ccDataGrid.DataSource = contactTable;
            bccDataGrid.DataSource = contactTable;
            DataGridViewColumn column = toDataGrid.Columns[0];
            column.Width = 42;
            column.HeaderText = "";
            column = ccDataGrid.Columns[0];
            column.Width = 42;
            column.HeaderText = "";
            column = bccDataGrid.Columns[0];
            column.Width = 42;
            column.HeaderText = "";

            ContactsService client = new ContactsService("William Randol-Gmail Icon Notifier-0.8 (RC1)");
            client.setUserCredentials(user, pass);
            string authToken = client.QueryAuthenticationToken(); // Authenticate the user immediately

            RequestSettings rs = new RequestSettings("Gmail Icon Notifier", user, pass);
            rs.AutoPaging = true;
            ContactsRequest cr = new ContactsRequest(rs);

            Feed<Contact> f = cr.GetContacts();

            foreach (Contact c in f.Entries)
            {
                foreach (EMail email in c.Emails)
                {
                    contactTable.Rows.Add(contactPhoto(c, authToken), c.Title, email.Address);
                }
            }
        }
        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;
            }
        }