从谷歌服务器对谷歌文档进行身份验证后,我执行了一个简单的 getResponse,但我收到了 400 错误请求。我不明白我哪里错了。示例代码如下
private void executeRefreshAlbums() {
HttpRequest request = transport.buildGetRequest();
request.url = GoogleDocsUrl.forDefaultPrivateFull();
System.out.println("URL = "+request.url);
try {
HttpResponse response = request.execute();
System.out.println("Response = "+response.getContent());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sysout 将正确的 url 打印为
03-12 17:36:59.573: INFO/System.out(451): URL = https://docs.google.com/feeds/default/private/full
但是当我这样做的时候,我得到了
03-12 17:43:41.360: WARN/System.err(3958): com.google.api.client.http.HttpResponseException: 400 Bad Request
03-12 17:43:41.415: WARN/System.err(3958): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:209)
03-12 17:43:41.415: WARN/System.err(3958): at com.example.Test.executeRefreshAlbums(Test.java:198)
03-12 17:43:41.415: WARN/System.err(3958): at com.example.Test.authenticated(Test.java:190)
03-12 17:43:41.415: WARN/System.err(3958): at com.example.Test.authenticatedClientLogin(Test.java:156)
03-12 17:43:41.415: WARN/System.err(3958): at com.example.Test.access$1(Test.java:153)
03-12 17:43:41.415: WARN/System.err(3958): at com.example.Test$2$1.run(Test.java:139)
03-12 17:43:41.415: WARN/System.err(3958): at android.os.Handler.handleCallback(Handler.java:587)
03-12 17:43:41.415: WARN/System.err(3958): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 17:43:41.415: WARN/System.err(3958): at android.os.Looper.loop(Looper.java:123)
03-12 17:43:41.415: WARN/System.err(3958): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-12 17:43:41.415: WARN/System.err(3958): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 17:43:41.415: WARN/System.err(3958): at java.lang.reflect.Method.invoke(Method.java:521)
03-12 17:43:41.422: WARN/System.err(3958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-12 17:43:41.422: WARN/System.err(3958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-12 17:43:41.422: WARN/System.err(3958): at dalvik.system.NativeStart.main(Native Method)
任何帮助将不胜感激。我已经试了一个小时了:-(
最佳答案
在另一个线程用户@Merlin provided解决类似问题的解决方案。他的解决方案是在请求中指定 API 版本号 3.0,如“Google Documents List Data API”文档中所述:
Important: If you make a request to the API that uses a feature only available in version 3, but forget to set the version, you will most likely receive a 400 Bad Request response. Since the URL structure is different between versions 2 and 3, this is a common mistake made by new users of the API.
To specify a version number, use the
GData-Version
HTTP header. The value of this header must be3.0
. The decimal and the zero are required.
GData-Version: 3.0
Alternatively, you can specify
v=3
as a query parameter in the URL. This is often needed when working behind a firewall that strips HTTP headers, or from some JavaScript requests. Use of the HTTP header is recommended when possible.
关于java - Google Docs 的 Google API,请求文档列表 -- 400 Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5286400/