开发者

How do you access Google service using a java client?

I am developing a java application which is registered on Google Apps Engine (GAE) with the name "searcegadget2". I have implemented 3-legged OAuth with OAuthHmacSha1Signer(). I am getting the Access Token properly & storing it on Session (session is enabled).

Next, I am calling the servlet using a link onclick event. This servlet is to access the Spreadsheet service using the accesstoken. My code is :

            GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
            oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
            oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
            oauthParameters.setOAuthType(OAuthParameters.OAuthType.THREE_LEGGED_OAUTH);
            oauthParameters.setScope("https://spreadsheets.google.com/feeds/");
            oauthParameters.setOAuthToken(request.getSession().getAttribute("oauth_token").toString());
            oauthParameters.setOAuthVerifier(request.getSession().getAttribute("oauth_verifier").toString());
            oauthParameters.setOAuthTokenSecret(request.getSession().getAttribute("oauth_token_secret").toString());



            out.println("Accessing Service");
            GoogleService googleService = new GoogleService("wise", "searceapps-searcegadget2-1");
            out.println("<br/>Setting Parameter");
            googleService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
            out.println("<br/>Setting URl");
            URL feedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
            out.println("<br/>Accessing ResultFeed");
            SpreadsheetFeed resultFeed = googleService.getFeed(feedUrl, SpreadsheetFeed.class);

            out.println("<div id='feed'>");
            out.println("Response Data:");
            out.println("=====================================================");
            out.println("| TITLE: " + resultFeed.getTitle().getPlainText());
            if (resultFeed.getEntries().isEmpty()) {
                out.println("|\tNo entries found.");
            } else {
                List<SpreadsheetEntry> spreadsheets = resultFeed.getEntries();
                for (int i = 0; i < spreadsheets.size(); i++) {
                    SpreadsheetEntry entry = spreadsheets.get(i);
                    System.out.println("\t" + entry.getTitle().getPlainText());
                }
            }
            out.println("=====================================================");
            out.println("</div>");

I have printed all the oauthParameter for testing & they are properly set. But, while calling this servlet, the only output that I am getting is :

Accessing Service

I have added, gdata-spreadsheet-3.0.jar but still it is not even allowing me to create the service object !!! What is the problem ?

Also, as per it given here, "Application names should preferably have the format [company-id]-[app-name]-[app-version]. The name will be used by the G开发者_如何学运维oogle servers to monitor the source of authentication." I don't know where I find my company id, but my app name is "searcegadget2" & version is "1".

For reference : Google Spreadsheets API Developer's Guide: Java, Using the Java Client Library, OAuth in the Google Data Protocol Client Libraries


Finally found,

Google Service has dependency on : guava as it is mentioned here.


here is an example that should help you.

I think you forgot to add some parameters like the scope.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜