- Exception when retrieving contacts from Google Contacts API
- public class Syncer {
- private String consumerKey = "XXXXXXXXX";
- private String consumerSecret = "XXXXXXXXX";
- private String masterUser = "XXXXXXXXX";
- private GoogleOAuthParameters oauthParameters;
- private ContactsService contactService;
- private AppsPropertyService appsService;
- public Syncer() throws IOException, ServiceException, OAuthException {
- oauthParameters = new GoogleOAuthParameters();
- oauthParameters.setOAuthConsumerKey(consumerKey);
- oauthParameters.setOAuthConsumerSecret(consumerSecret);
- oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
- contactService = new ContactsService(consumerKey);
- contactService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
- appsService = new AppsPropertyService(consumerKey);
- appsService.setUserCredentials("XXXXXXX", "XXXXXXXXX");
- sync();
- }
- public List<ContactGroupEntry> retrieveGroups(String user) throws IOException, ServiceException {
- URL feedUrl = new URL("https://www.google.com/m8/feeds/groups/" + user + "/full?xoauth_requestor_id=" + masterUser);
- ContactGroupFeed resultFeed = contactService.getFeed(feedUrl, ContactGroupFeed.class);
- return resultFeed.getEntries();
- }
- public List<ContactEntry> retrieveGroupContacts(ContactGroupEntry group) throws IOException, ServiceException {
- URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full?group=" + group.getId() + "&xoauth_requestor_id=" + masterUser);
- ContactFeed resultFeed = contactService.getFeed(feedUrl, ContactFeed.class);
- return resultFeed.getEntries();
- }
- public List<GenericEntry> getAllUsers() throws AppsForYourDomainException, MalformedURLException, IOException, ServiceException {
- GenericFeed feed = appsService.getFeed(new URL("https://apps-apis.google.com/a/feeds/user/2.0/" + consumerKey), GenericFeed.class);
- List<GenericEntry> users = feed.getEntries();
- Link next;
- while((next = feed.getNextLink()) != null) {
- feed = appsService.getFeed(new URL(next.getHref()), GenericFeed.class);
- users.addAll(feed.getEntries());
- }
- return users;
- }
- public void sync() throws IOException, ServiceException {
- Map<ContactGroupEntry, List<ContactEntry>> masterContactsList = new HashMap<ContactGroupEntry, List<ContactEntry>>();
- List<ContactGroupEntry> masterGroups = retrieveGroups(masterUser);
- ContactGroupFeed groupBatchCreate = new ContactGroupFeed();
- for(ContactGroupEntry group : masterGroups) {
- if(!group.hasSystemGroup() && group.getTitle().getPlainText().startsWith("SOG:")) {
- List<ContactEntry> contacts = retrieveGroupContacts(group);
- masterContactsList.put(group, contacts);
- ContactGroupEntry createContactGroup = new ContactGroupEntry();
- createContactGroup.setTitle(new PlainTextConstruct(group.getPlainTextContent()));
- BatchUtils.setBatchId(createContactGroup, "create");
- BatchUtils.setBatchOperationType(createContactGroup, BatchOperationType.INSERT);
- groupBatchCreate.getEntries().add(createContactGroup);
- }
- }
- List<GenericEntry> users = getAllUsers();
- for(GenericEntry user : users) {
- List<ContactGroupEntry> userGroups = retrieveGroups(user.getProperty("userEmail"));
- for(ContactGroupEntry group : userGroups) {
- if(group.getTitle().getPlainText().startsWith("SOG:")) {
- group.delete();
- }
- }
- contactService.batch(new URL("https://www.google.com/m8/feeds/groups/" + user.getProperty("userEmail") + "/full/batch"), groupBatchCreate);
- }
- }
- public static void main(String[] args) throws IOException, ServiceException, OAuthException {
- new Syncer();
- }