开发者

Gae Java - After getting Authorization Token (with ClientLogin) cannot fetch spreadsheet feed url with 2 http requests

开发者 https://www.devze.com 2023-02-08 08:50 出处:网络
I have a problem using google apps engine with google spreadsheet. I Get the authorization token with another servlet (by google ClientLogin) and then i try to get the spreadsheet feed xml with GET re

I have a problem using google apps engine with google spreadsheet. I Get the authorization token with another servlet (by google ClientLogin) and then i try to get the spreadsheet feed xml with GET request and Authorization header (as described by googl开发者_StackOverflow中文版e documentation).

My servlet look like this:

public class My2Servlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();

    HTTPRequest tokenRequest = new HTTPRequest(new URL("http://localhost:8888/myGae/getauthtoken"), HTTPMethod.GET);
    HTTPResponse tokenResponse = urlFetchService.fetch(tokenRequest);

    String token = Utils.getText(tokenResponse.getContent()); /*this token is OK*/

    HTTPRequest spreadsheetFeedRequest = new HTTPRequest(new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full"), HTTPMethod.GET);
    spreadsheetFeedRequest.setHeader(new HTTPHeader("Authorization", "GoogleLogin auth=" + token));
    HTTPResponse spreadsheetFeedResponse = urlFetchService.fetch(spreadsheetFeedRequest); /*here the problems!!!*/


    String spreadsheetFeed = Utils.getText(spreadsheetFeedResponse.getContent());

    resp.setContentType("text/plain");
    resp.getWriter().println(spreadsheetFeed);
} 

}

I can correctly have the token but when i try to do the second request to have the spreadsheet feed i have the error 400 Bad Request and if i retry to reload this error:

java.io.IOException: Could not fetch URL: https://spreadsheets.google.com/feeds/spreadsheets/private/full

It seems that only the first request work... in fact if I comment the second request and get the token then comment the first request and execute the second request with token hand-written I correctly have the spreadsheet feed xml output...

Why can't I perform two subsequent requests?


I have implemented google-oauth (3-legged) & used gdata client library. I am explaining only for FYI, as this not the solution but just a suggestion.

You can download it from here. See the documentation.

Then use the following code :

Get the Spreadsheet feed :

SpreadsheetFeed resultFeed = googleService.getFeed(feedUrl, SpreadsheetFeed.class);
if (resultFeed.getEntries().isEmpty()) {
   out.println("<br/>|\tNo entries found.");
} else {
   List<SpreadsheetEntry> spreadsheets = resultFeed.getEntries();
   for (int i = 0; i < spreadsheets.size(); i++) {
       SpreadsheetEntry entry = spreadsheets.get(i);
       out.println("<br/>" + entry.getTitle().getPlainText());
   }
}
0

精彩评论

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