我在GAE中通过servlet从Google API获得了404错误,但是通过浏览器获得了正确的响应

时间:2015-03-12 09:09:14

标签: google-app-engine servlets google-api

我可以通过网络浏览器从Google API获得正确的回复。据说, 当我访问 https://www.googleapis.com/blogger/v3/blogs/4871538073781423026?access_token=XXXXXXXX 通过一些网络浏览器,然后获得正确的响应,如

{
 "kind": "blogger#blog",
 "id": "4871538073781423026",
 "name": "SoffeeShop",
 "description": "",
 "published": "2012-10-24T23:55:32+09:00",
 "updated": "2015-03-04T14:31:00+09:00",
 "url": "http://soffeeshop.blogspot.com/",
 "selfLink": "https://www.googleapis.com/blogger/v3/blogs/4871538073781423026",
 "posts": {
  "totalItems": 58,
  "selfLink": "https://www.googleapis.com/blogger/v3/blogs/4871538073781423026/posts"
 },
 "pages": {
  "totalItems": 0,
  "selfLink": "https://www.googleapis.com/blogger/v3/blogs/4871538073781423026/pages"
 },
 "locale": {
  "language": "en",
  "country": "",
  "variant": ""
 }
}

虽然,当我在GAE中使用servlet做REST时,比如

URL url = new URL("https://www.googleapis.com/blogger/v3/blogs/4871538073781423026?access_token=XXXXXXXXX");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("X-Appengine-Inbound-Appid", "XXXXX");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Host", "googleapis.com");

URL url = new URL("https://www.googleapis.com/blogger/v3/blogs/4871538073781423026);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", "Bearer " + access_token); 
connection.setRequestProperty("X-Appengine-Inbound-Appid", "XXXXX");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Host", "googleapis.com");

我刚收到“404 Not Found”错误。

我可以在GAE中的同一个servlet中使用相同的HttpURLConnection使用OAuth2获取access_token,我无法访问Google API。因此,这意味着Http Connections可以很好地访问其他服务器,但看起来Google API拒绝从GAE中的servlet访问。

如何通过GAE中的servlet访问Google API或Blogger API?请帮帮我。

2 个答案:

答案 0 :(得分:1)

您可能需要在Google开发者控制台的GAE项目中启用博客API,它可能会问您一些问题,例如"您希望如何使用Blogger API?"。

答案 1 :(得分:1)

您是否向GAE应用提供了访问Blogger API的权限。我想当您在应用的开发者控制台中生成clientId时,您必须在重定向URIS中提供您的应用名称http://app-id.appspot.com。我认为它会解决你的问题。