public static async Task SyncGoogleContactsAsync() { var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( new Uri("ms-appx:///Assets/client_secrets.json"), new[] { ContactsService.Scope.FullContacts, Oauth2Service.Scope.UserinfoProfile, Oauth2Service.Scope.UserinfoEmail }, "user", CancellationToken.None); await SyncProfileContactImage(); var services = new ContactsService(new BaseClientService.Initializer() { ApplicationName = "WhassApp Windows 8", HttpClientInitializer = credential }); var oauthSerivce = new Oauth2Service(new BaseClientService.Initializer() { ApplicationName = "WhassApp Windows 8", HttpClientInitializer = credential }); var userInfo = await oauthSerivce.Userinfo.Get().ExecuteAsync(); List <string> numberList = new List <string>(); try { PhoneNumbers.PhoneNumberUtil util = PhoneNumbers.PhoneNumberUtil.GetInstance(); ContactList response = await services.Contact.Get(userInfo.Email).ExecuteAsync(); foreach (var c in response.Feed.Entries) { if (c.PhoneNumbers != null) { string phoneNumber = String.Empty; if (c.PhoneNumbers.FirstOrDefault().Text != null) { try { PhoneNumbers.PhoneNumber num = util.Parse(c.PhoneNumbers.FirstOrDefault().Text, "ID"); phoneNumber = num.CountryCode.ToString() + num.NationalNumber.ToString(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception was thrown: " + ex.Message); continue; } } AddContact(new Contacts() { name = c.Name.Text, phoneNumber = phoneNumber }); var check = GetContactByPhoneNumber(phoneNumber); if (check == null) { numberList.Add(phoneNumber); } } } } catch (Google.GoogleApiException ex) { } SocketInstance.Instance.OnGetSyncResult += Instance_OnGetSyncResult; SocketInstance.Instance.SendSync(numberList.ToArray(), Libs.Constants.Enums.SyncMode.Delta, Libs.Constants.Enums.SyncContext.Background); }
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 exe) { 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(); }