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

泄露秘密 提交于 2019-12-01 08:41:13

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);
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!