Guest User

Untitled

a guest
May 4th, 2012
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. Exception when retrieving contacts from Google Contacts API
  2. public class Syncer {
  3.  
  4. private String consumerKey = "XXXXXXXXX";
  5. private String consumerSecret = "XXXXXXXXX";
  6. private String masterUser = "XXXXXXXXX";
  7. private GoogleOAuthParameters oauthParameters;
  8. private ContactsService contactService;
  9. private AppsPropertyService appsService;
  10.  
  11.  
  12. public Syncer() throws IOException, ServiceException, OAuthException {
  13. oauthParameters = new GoogleOAuthParameters();
  14. oauthParameters.setOAuthConsumerKey(consumerKey);
  15. oauthParameters.setOAuthConsumerSecret(consumerSecret);
  16. oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
  17. contactService = new ContactsService(consumerKey);
  18. contactService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
  19. appsService = new AppsPropertyService(consumerKey);
  20. appsService.setUserCredentials("XXXXXXX", "XXXXXXXXX");
  21. sync();
  22. }
  23.  
  24. public List<ContactGroupEntry> retrieveGroups(String user) throws IOException, ServiceException {
  25. URL feedUrl = new URL("https://www.google.com/m8/feeds/groups/" + user + "/full?xoauth_requestor_id=" + masterUser);
  26. ContactGroupFeed resultFeed = contactService.getFeed(feedUrl, ContactGroupFeed.class);
  27. return resultFeed.getEntries();
  28. }
  29.  
  30. public List<ContactEntry> retrieveGroupContacts(ContactGroupEntry group) throws IOException, ServiceException {
  31. URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full?group=" + group.getId() + "&xoauth_requestor_id=" + masterUser);
  32. ContactFeed resultFeed = contactService.getFeed(feedUrl, ContactFeed.class);
  33. return resultFeed.getEntries();
  34. }
  35.  
  36. public List<GenericEntry> getAllUsers() throws AppsForYourDomainException, MalformedURLException, IOException, ServiceException {
  37. GenericFeed feed = appsService.getFeed(new URL("https://apps-apis.google.com/a/feeds/user/2.0/" + consumerKey), GenericFeed.class);
  38. List<GenericEntry> users = feed.getEntries();
  39. Link next;
  40. while((next = feed.getNextLink()) != null) {
  41. feed = appsService.getFeed(new URL(next.getHref()), GenericFeed.class);
  42. users.addAll(feed.getEntries());
  43. }
  44. return users;
  45. }
  46.  
  47.  
  48. public void sync() throws IOException, ServiceException {
  49. Map<ContactGroupEntry, List<ContactEntry>> masterContactsList = new HashMap<ContactGroupEntry, List<ContactEntry>>();
  50. List<ContactGroupEntry> masterGroups = retrieveGroups(masterUser);
  51. ContactGroupFeed groupBatchCreate = new ContactGroupFeed();
  52.  
  53. for(ContactGroupEntry group : masterGroups) {
  54. if(!group.hasSystemGroup() && group.getTitle().getPlainText().startsWith("SOG:")) {
  55. List<ContactEntry> contacts = retrieveGroupContacts(group);
  56. masterContactsList.put(group, contacts);
  57.  
  58. ContactGroupEntry createContactGroup = new ContactGroupEntry();
  59. createContactGroup.setTitle(new PlainTextConstruct(group.getPlainTextContent()));
  60. BatchUtils.setBatchId(createContactGroup, "create");
  61. BatchUtils.setBatchOperationType(createContactGroup, BatchOperationType.INSERT);
  62. groupBatchCreate.getEntries().add(createContactGroup);
  63. }
  64. }
  65.  
  66. List<GenericEntry> users = getAllUsers();
  67. for(GenericEntry user : users) {
  68. List<ContactGroupEntry> userGroups = retrieveGroups(user.getProperty("userEmail"));
  69. for(ContactGroupEntry group : userGroups) {
  70. if(group.getTitle().getPlainText().startsWith("SOG:")) {
  71. group.delete();
  72. }
  73. }
  74.  
  75. contactService.batch(new URL("https://www.google.com/m8/feeds/groups/" + user.getProperty("userEmail") + "/full/batch"), groupBatchCreate);
  76. }
  77. }
  78.  
  79.  
  80. public static void main(String[] args) throws IOException, ServiceException, OAuthException {
  81. new Syncer();
  82. }
Advertisement
Add Comment
Please, Sign In to add comment