Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 9.38 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Batch Update to Spreadsheet with Google GData API Using 2 Legged OAUTH / OPEN ID Domain Account
  2. final String SCOPE = "https://spreadsheets.google.com/feeds/spreadsheets/private/full";
  3.         SpreadsheetService spreadsheetService;
  4.         String consumerKey = getInitParameter("consumer_key");
  5.         String consumerSecret = getInitParameter("consumer_secret");
  6.         GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
  7.         oauthParameters.setOAuthConsumerKey(consumerKey);
  8.         oauthParameters.setOAuthConsumerSecret(consumerSecret);
  9.         oauthParameters.setOAuthType(OAuthParameters.OAuthType.TWO_LEGGED_OAUTH);
  10.         oauthParameters.setScope(SCOPE);
  11.         OAuthSigner signer = new OAuthHmacSha1Signer();
  12.         spreadsheetService = new SpreadsheetService("nimbits-com");
  13.         String title = entity.getName().getValue();
  14.  
  15.         try {
  16.  
  17.             spreadsheetService.setOAuthCredentials(oauthParameters, signer);
  18.             spreadsheetService.setProtocolVersion(SpreadsheetService.Versions.V3);
  19.             SpreadsheetQuery query = new SpreadsheetQuery(new URL(SCOPE));
  20.             query.addCustomParameter(new Query.CustomParameter("xoauth_requestor_id", user.getEmail().getValue()));
  21.             query.setTitleQuery(title);
  22.             SpreadsheetFeed feed =  spreadsheetService.query(query, SpreadsheetFeed.class);
  23.  
  24.  
  25. //works fine up to this point, I get the feed and spreadsheet.
  26.  
  27.             if (feed != null && ! feed.getEntries().isEmpty()) {
  28.  
  29.                 com.google.gdata.data.spreadsheet.SpreadsheetEntry wsEntry = feed.getEntries().get(0);
  30.                 WorksheetEntry sheet = wsEntry.getWorksheets().get(0);
  31.                 CellFeed batchRequest = new CellFeed();
  32.                 String batchId = "R" + 2 + "C" + 1;
  33.  
  34.  
  35.                 URL entryUrl = new URL(sheet.getCellFeedUrl().toString() + "/" + batchId);
  36.  
  37. //Invalid TOKEN error here, trying to get the entry.
  38. CellEntry batchOperation = spreadsheetService.getEntry(entryUrl, CellEntry.class);
  39.                 batchOperation.setService(spreadsheetService);
  40.                 batchOperation.changeInputValueLocal("test");
  41.                 BatchUtils.setBatchId(batchOperation, batchId);
  42.                 BatchUtils.setBatchOperationType(batchOperation, BatchOperationType.UPDATE);
  43.                 batchRequest.getEntries().add(batchOperation);
  44.  
  45.  
  46.                 CellFeed cellFeed = spreadsheetService.getFeed(sheet.getCellFeedUrl(), CellFeed.class);
  47.                 Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
  48.                 URL batchUrl = new URL(batchLink.getHref());
  49.                 spreadsheetService.batch(batchUrl, batchRequest);
  50.             }
  51.         } catch (MalformedURLException e) {
  52.             LogHelper.logException(this.getClass(), e);
  53.             throw new NimbitsException(e);
  54.  
  55.         } catch (ServiceException e) {
  56.             LogHelper.logException(this.getClass(), e);
  57.             throw new NimbitsException(e);
  58.         } catch (IOException e) {
  59.             LogHelper.logException(this.getClass(), e);
  60.             throw new NimbitsException(e);
  61.         } catch (OAuthException e) {
  62.             LogHelper.logException(this.getClass(), e);
  63.             throw new NimbitsException(e);
  64.         }
  65.        
  66. @Override
  67. public void addSpreadsheetHeader(Entity entity) throws NimbitsException {
  68.     final User user = UserServiceFactory.getServerInstance().getHttpRequestUser(
  69.             this.getThreadLocalRequest());
  70.  
  71.  
  72.     SpreadsheetService spreadsheetService;
  73.     String consumerKey = getInitParameter("consumer_key");
  74.     String consumerSecret = getInitParameter("consumer_secret");
  75.     GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
  76.     oauthParameters.setOAuthConsumerKey(consumerKey);
  77.     oauthParameters.setOAuthConsumerSecret(consumerSecret);
  78.     spreadsheetService = new SpreadsheetService("nimbits-com");
  79.  
  80.  
  81.     //  SpreadsheetEntry entry = new SpreadsheetEntry();
  82.     String title = entity.getName().getValue();
  83.     //  entry.setTitle(TextConstruct.plainText(title));
  84.  
  85.  
  86.  
  87.     try {
  88.         spreadsheetService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
  89.         SpreadsheetQuery query = new SpreadsheetQuery(new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full"));
  90.         query.addCustomParameter(new Query.CustomParameter("xoauth_requestor_id", user.getEmail().getValue()));
  91.  
  92.         query.setTitleQuery(title);
  93.         SpreadsheetFeed feed =  spreadsheetService.query(query, SpreadsheetFeed.class);
  94.         if (feed != null && ! feed.getEntries().isEmpty()) {
  95.             com.google.gdata.data.spreadsheet.SpreadsheetEntry wsEntry = feed.getEntries().get(0);
  96.             WorksheetEntry sheet = wsEntry.getWorksheets().get(0);
  97.             URL cellFeedUrl= sheet.getCellFeedUrl ();
  98.             CellFeed cellFeed= spreadsheetService.getFeed (cellFeedUrl, CellFeed.class);
  99.             CellEntry cellEntry;
  100.  
  101.             cellEntry= new CellEntry (1, 1, "Timestamp");
  102.             cellFeed.insert (cellEntry);
  103.  
  104.             cellEntry= new CellEntry (1, 2, "Value");
  105.             cellFeed.insert (cellEntry);
  106.  
  107.             cellEntry= new CellEntry (1, 3, "Latitude");
  108.             cellFeed.insert (cellEntry);
  109.  
  110.             cellEntry= new CellEntry (1, 4, "Longitude");
  111.             cellFeed.insert (cellEntry);
  112.  
  113.             cellEntry= new CellEntry (1, 5, "Annotation");
  114.             cellFeed.insert (cellEntry);
  115.  
  116.             cellEntry= new CellEntry (1, 6, "Data");
  117.             cellFeed.insert (cellEntry);
  118.  
  119.         }
  120.  
  121.  
  122.  
  123.     } catch (MalformedURLException e) {
  124.         LogHelper.logException(this.getClass(), e);
  125.         throw new NimbitsException(e);
  126.  
  127.     } catch (ServiceException e) {
  128.         LogHelper.logException(this.getClass(), e);
  129.         throw new NimbitsException(e);
  130.     } catch (IOException e) {
  131.         LogHelper.logException(this.getClass(), e);
  132.         throw new NimbitsException(e);
  133.     } catch (OAuthException e) {
  134.         LogHelper.logException(this.getClass(), e);
  135.         throw new NimbitsException(e);
  136.     }
  137.  
  138.  
  139. }
  140.        
  141. @Override
  142.     public void dumpValues(final Entity entity, int count) throws NimbitsException {
  143.         String[][] values = {   {"1", "2", "3", "4", "5"},
  144.                 {"a", "b", "c", "d", "e"},
  145.                 {"dummy", "foo", "bar", "x", "y"}};
  146.  
  147.  
  148.         final User user = //where you store your user
  149.  
  150.  
  151.         SpreadsheetService spreadsheetService;
  152.         String consumerKey = getInitParameter("consumer_key");
  153.         String consumerSecret = getInitParameter("consumer_secret");
  154.         GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
  155.         oauthParameters.setOAuthConsumerKey(consumerKey);
  156.         oauthParameters.setOAuthConsumerSecret(consumerSecret);
  157.         spreadsheetService = new SpreadsheetService("nimbits-com");
  158.         spreadsheetService.setProtocolVersion(SpreadsheetService.Versions.V1);
  159.  
  160.  
  161.         try {
  162.             spreadsheetService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
  163.             String key = String.valueOf(this.getThreadLocalRequest().getSession().getAttribute("docId"));
  164.             FeedURLFactory urlFactory = FeedURLFactory.getDefault();
  165.             URL cellFeedUrl = urlFactory.getCellFeedUrl(key, "od6", "private", "full");
  166.  
  167.  
  168.             CellQuery q = new CellQuery(cellFeedUrl);
  169.             //CellQuery q = new CellQuery(worksheet.getCellFeedUrl());
  170.             q.setMinimumRow(1);
  171.             q.setMaximumRow(1 + values.length);
  172.             q.setMinimumCol(1);
  173.             q.setMaximumCol(values[0].length);
  174.             q.setReturnEmpty(true);
  175.             q.addCustomParameter(new Query.CustomParameter("xoauth_requestor_id", user.getEmail().getValue()));
  176.             CellFeed cellFeed = spreadsheetService.query(q, CellFeed.class);
  177.  
  178.             CellFeed batchRequestFeed = new CellFeed();
  179.  
  180.             // set values for each cell
  181.             int currentCellEntry=0;
  182.             for (int i=0; i < values.length; i++) {
  183.                 for (int j=0; j < values[i].length; j++) {
  184.  
  185.                     CellEntry entry = new CellEntry(cellFeed.getEntries().get(currentCellEntry));
  186.                     entry.changeInputValueLocal(values[i][j]);
  187.                     BatchUtils.setBatchId(entry, (new Integer(currentCellEntry)).toString());
  188.                     BatchUtils.setBatchOperationType(entry, BatchOperationType.UPDATE);
  189.                     batchRequestFeed.getEntries().add(entry);
  190.                     currentCellEntry++;
  191.                 }
  192.             }
  193.  
  194.             // upload cells
  195.             Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
  196.             spreadsheetService.setHeader("If-Match", "*");
  197.  
  198.             CellFeed batchResponse = spreadsheetService.batch(new URL(batchLink.getHref() ), batchRequestFeed);
  199.             spreadsheetService.setHeader("If-Match", null);
  200.             for (CellEntry entry : batchResponse.getEntries()) {
  201.                 if (!BatchUtils.isSuccess(entry)) {
  202.  
  203.                     BatchStatus status = BatchUtils.getBatchStatus(entry);
  204.                     throw new NimbitsException(BatchUtils.getBatchId(entry) + " " + status.getReason() + " " + status.getContent());
  205.                 }
  206.             }
  207.         } catch (IOException e) {
  208.             LogHelper.logException(this.getClass(), e);
  209.             throw new NimbitsException(e);
  210.         } catch (ServiceException e) {
  211.             LogHelper.logException(this.getClass(), e);
  212.             throw new NimbitsException(e);
  213.         } catch (OAuthException e) {
  214.             LogHelper.logException(this.getClass(), e);
  215.             throw new NimbitsException(e);
  216.         }