使用2条腿式OAUTH/OPEN ID域帐户,使用Google GData API批量更新电子表格 [英] Batch Update to Spreadsheet with Google GData API Using 2 Legged OAUTH / OPEN ID Domain Account

查看:94
本文介绍了使用2条腿式OAUTH/OPEN ID域帐户,使用Google GData API批量更新电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,周末就是这个镜头-这是我在周日晚上的最后一次求助电话.

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

必须使用2条腿式OAUTH方法的Google Marketplace应用需要将5万条记录写入Google文档电子表格.我能够创建,调整大小,重命名并将记录写入电子表格.单独写入100行最多需要一分钟,因此我必须使用批处理更新.无法批量更新工作.下面的代码是我的最佳尝试-我不断收到令牌无效-无效令牌401".所有示例代码都针对三个问题进行了验证-但由于它是一个市场应用程序,因此我无法提示用户批准该应用程序

Google Marketplace app that must use 2 Legged OAUTH aproach needs to write 50K records to a Google Doc Spreadsheet. I'm able to create, resize, rename and write records to a spreadsheet. It takes up to a minute to write 100 rows individually, so I have to use a batch update. Can't get batch update to work. The below code is my best attempt - I keep getting "Token invalid - Invalid token 401". All of the sample code out there is for three legged oauth - but I can't prompt the user to approve the app since it's a marketplace app

找到使用openid和2LO进行批处理电子表格更新的示例非常好

It would be great to find an example of batch spreadsheet updates using openid and 2LO

      final String SCOPE = "https://spreadsheets.google.com/feeds/spreadsheets/private/full";
        SpreadsheetService spreadsheetService;
        String consumerKey = getInitParameter("consumer_key");
        String consumerSecret = getInitParameter("consumer_secret");
        GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
        oauthParameters.setOAuthConsumerKey(consumerKey);
        oauthParameters.setOAuthConsumerSecret(consumerSecret);
        oauthParameters.setOAuthType(OAuthParameters.OAuthType.TWO_LEGGED_OAUTH);
        oauthParameters.setScope(SCOPE);
        OAuthSigner signer = new OAuthHmacSha1Signer();
        spreadsheetService = new SpreadsheetService("nimbits-com");
        String title = entity.getName().getValue();

        try {

            spreadsheetService.setOAuthCredentials(oauthParameters, signer);
            spreadsheetService.setProtocolVersion(SpreadsheetService.Versions.V3);
            SpreadsheetQuery query = new SpreadsheetQuery(new URL(SCOPE));
            query.addCustomParameter(new Query.CustomParameter("xoauth_requestor_id", user.getEmail().getValue()));
            query.setTitleQuery(title);
            SpreadsheetFeed feed =  spreadsheetService.query(query, SpreadsheetFeed.class);


//works fine up to this point, I get the feed and spreadsheet.

            if (feed != null && ! feed.getEntries().isEmpty()) {

                com.google.gdata.data.spreadsheet.SpreadsheetEntry wsEntry = feed.getEntries().get(0);
                WorksheetEntry sheet = wsEntry.getWorksheets().get(0);
                CellFeed batchRequest = new CellFeed();
                String batchId = "R" + 2 + "C" + 1;


                URL entryUrl = new URL(sheet.getCellFeedUrl().toString() + "/" + batchId);

//Invalid TOKEN error here, trying to get the entry. 
CellEntry batchOperation = spreadsheetService.getEntry(entryUrl, CellEntry.class);
                batchOperation.setService(spreadsheetService);
                batchOperation.changeInputValueLocal("test");
                BatchUtils.setBatchId(batchOperation, batchId);
                BatchUtils.setBatchOperationType(batchOperation, BatchOperationType.UPDATE);
                batchRequest.getEntries().add(batchOperation);


                CellFeed cellFeed = spreadsheetService.getFeed(sheet.getCellFeedUrl(), CellFeed.class);
                Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
                URL batchUrl = new URL(batchLink.getHref());
                spreadsheetService.batch(batchUrl, batchRequest);
            }
        } catch (MalformedURLException e) {
            LogHelper.logException(this.getClass(), e);
            throw new NimbitsException(e);

        } catch (ServiceException e) {
            LogHelper.logException(this.getClass(), e);
            throw new NimbitsException(e);
        } catch (IOException e) {
            LogHelper.logException(this.getClass(), e);
            throw new NimbitsException(e);
        } catch (OAuthException e) {
            LogHelper.logException(this.getClass(), e);
            throw new NimbitsException(e);
        }

此示例仅尝试批量写入一条记录.我已经调整了电子表格的大小,看起来也不错.我看不到如何设置令牌或如何使用2LO完成此请求.我尝试将xoauth_requestor_id添加到网址中.

This sample just tries to write one record in a batch. I've resized the spreadsheet and it looks ok as well. I don't see how to set the token, or complete this request with 2LO. I've tried adding xoauth_requestor_id to the urls.

此代码段可以完美运行,并使用我拥有的2LO身份验证将1行添加到现有电子表格中.我需要对其进行修改以快速插入50,000行.

This snippet of code works perfectly and adds 1 row to an existing spreadsheet with the 2LO authentication I have. I need it modified to instert 50,000 rows quickly.

 @Override
public void addSpreadsheetHeader(Entity entity) throws NimbitsException {
    final User user = UserServiceFactory.getServerInstance().getHttpRequestUser(
            this.getThreadLocalRequest());


    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");


    //  SpreadsheetEntry entry = new SpreadsheetEntry();
    String title = entity.getName().getValue();
    //  entry.setTitle(TextConstruct.plainText(title));



    try {
        spreadsheetService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
        SpreadsheetQuery query = new SpreadsheetQuery(new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full"));
        query.addCustomParameter(new Query.CustomParameter("xoauth_requestor_id", user.getEmail().getValue()));

        query.setTitleQuery(title);
        SpreadsheetFeed feed =  spreadsheetService.query(query, SpreadsheetFeed.class);
        if (feed != null && ! feed.getEntries().isEmpty()) {
            com.google.gdata.data.spreadsheet.SpreadsheetEntry wsEntry = feed.getEntries().get(0);
            WorksheetEntry sheet = wsEntry.getWorksheets().get(0);
            URL cellFeedUrl= sheet.getCellFeedUrl ();
            CellFeed cellFeed= spreadsheetService.getFeed (cellFeedUrl, CellFeed.class);
            CellEntry cellEntry;

            cellEntry= new CellEntry (1, 1, "Timestamp");
            cellFeed.insert (cellEntry);

            cellEntry= new CellEntry (1, 2, "Value");
            cellFeed.insert (cellEntry);

            cellEntry= new CellEntry (1, 3, "Latitude");
            cellFeed.insert (cellEntry);

            cellEntry= new CellEntry (1, 4, "Longitude");
            cellFeed.insert (cellEntry);

            cellEntry= new CellEntry (1, 5, "Annotation");
            cellFeed.insert (cellEntry);

            cellEntry= new CellEntry (1, 6, "Data");
            cellFeed.insert (cellEntry);

        }



    } catch (MalformedURLException e) {
        LogHelper.logException(this.getClass(), e);
        throw new NimbitsException(e);

    } catch (ServiceException e) {
        LogHelper.logException(this.getClass(), e);
        throw new NimbitsException(e);
    } catch (IOException e) {
        LogHelper.logException(this.getClass(), e);
        throw new NimbitsException(e);
    } catch (OAuthException e) {
        LogHelper.logException(this.getClass(), e);
        throw new NimbitsException(e);
    }


}

推荐答案

在这里,很多小东西都没有记录在案-但是两腿的auth转储到了电子表格中:

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

从各种来源提取了一些示例代码以将其组合在一起.创建新文档.getId时,文档密钥来自返回的对象.

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);
        }

这篇关于使用2条腿式OAUTH/OPEN ID域帐户,使用Google GData API批量更新电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆