我在使用谷歌应用程序引擎和谷歌电子表格时遇到问题。我使用另一个 servlet(通过 google ClientLogin)获取授权令牌,然后尝试使用 GET 请求和 Authorization 标头获取电子表格提要 xml(如 google 文档所述)。
我的 servlet 如下所示:
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);
}
}
我可以正确获得令牌,但是当我尝试执行第二个请求以获取电子表格提要时,我收到错误 400 Bad Request 并且如果我重试重新加载此错误:
java.io.IOException: Could not fetch URL: https://spreadsheets.google.com/feeds/spreadsheets/private/full
似乎只有第一个请求有效......事实上,如果我评论第二个请求并获取令牌然后评论第一个请求并使用手写令牌执行第二个请求我正确地拥有电子表格提要 xml 输出......
为什么我不能执行两个后续请求?