Batch Update to Spreadsheet with Google GData API Using 2 Legged OAUTH / OPEN ID Domain Account

后端 未结 1 1725
情书的邮戳
情书的邮戳 2021-01-14 13:38

Ok, weekend is shot on this - here is my last ditch sunday night call for help.

Google Marketplace app that must use 2 Legged OAUTH aproach needs to write 50K recor

1条回答
  •  心在旅途
    2021-01-14 14:29

    Here it is, a lot of little things undocumented - but two legged auth dump to a spreadsheet:

    Pulled some sample code from various sources to put it together. The document key comes from the returned object when you create a new document .getId.

      @Override
        public void dumpValues(final Entity entity, int count) throws NimbitsException {
            String[][] values = {   {"1", "2", "3", "4", "5"},
                    {"a", "b", "c", "d", "e"},
                    {"dummy", "foo", "bar", "x", "y"}};
    
    
            final User user = //where you store your user
    
    
            SpreadsheetService spreadsheetService;
            String consumerKey = getInitParameter("consumer_key");
            String consumerSecret = getInitParameter("consumer_secret");
            GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
            oauthParameters.setOAuthConsumerKey(consumerKey);
            oauthParameters.setOAuthConsumerSecret(consumerSecret);
            spreadsheetService = new SpreadsheetService("nimbits-com");
            spreadsheetService.setProtocolVersion(SpreadsheetService.Versions.V1);
    
    
            try {
                spreadsheetService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
                String key = String.valueOf(this.getThreadLocalRequest().getSession().getAttribute("docId"));
                FeedURLFactory urlFactory = FeedURLFactory.getDefault();
                URL cellFeedUrl = urlFactory.getCellFeedUrl(key, "od6", "private", "full");
    
    
                CellQuery q = new CellQuery(cellFeedUrl);
                //CellQuery q = new CellQuery(worksheet.getCellFeedUrl());
                q.setMinimumRow(1);
                q.setMaximumRow(1 + values.length);
                q.setMinimumCol(1);
                q.setMaximumCol(values[0].length);
                q.setReturnEmpty(true);
                q.addCustomParameter(new Query.CustomParameter("xoauth_requestor_id", user.getEmail().getValue()));
                CellFeed cellFeed = spreadsheetService.query(q, CellFeed.class);
    
                CellFeed batchRequestFeed = new CellFeed();
    
                // set values for each cell
                int currentCellEntry=0;
                for (int i=0; i < values.length; i++) {
                    for (int j=0; j < values[i].length; j++) {
    
                        CellEntry entry = new CellEntry(cellFeed.getEntries().get(currentCellEntry));
                        entry.changeInputValueLocal(values[i][j]);
                        BatchUtils.setBatchId(entry, (new Integer(currentCellEntry)).toString());
                        BatchUtils.setBatchOperationType(entry, BatchOperationType.UPDATE);
                        batchRequestFeed.getEntries().add(entry);
                        currentCellEntry++;
                    }
                }
    
                // upload cells
                Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
                spreadsheetService.setHeader("If-Match", "*");
    
                CellFeed batchResponse = spreadsheetService.batch(new URL(batchLink.getHref() ), batchRequestFeed);
                spreadsheetService.setHeader("If-Match", null);
                for (CellEntry entry : batchResponse.getEntries()) {
                    if (!BatchUtils.isSuccess(entry)) {
    
                        BatchStatus status = BatchUtils.getBatchStatus(entry);
                        throw new NimbitsException(BatchUtils.getBatchId(entry) + " " + status.getReason() + " " + status.getContent());
                    }
                }
            } catch (IOException e) {
                LogHelper.logException(this.getClass(), e);
                throw new NimbitsException(e);
            } catch (ServiceException e) {
                LogHelper.logException(this.getClass(), e);
                throw new NimbitsException(e);
            } catch (OAuthException e) {
                LogHelper.logException(this.getClass(), e);
                throw new NimbitsException(e);
            }
    

    0 讨论(0)
提交回复
热议问题