我尝试使用OAuth Playground,它运行良好,并返回了我所有的10个任务列表。但以下是我的 Java 代码,它返回了我的有线响应。
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
String clientId = "[MYCLIENTID].apps.googleusercontent.com";
String clientSecret = "[MYCLIENTSECRET]";
String refreshToken = "1/P_mWwxnZEJHlDLiUs_BzMIvx6MDewDmvrgx-LNTfib0";
GoogleRefreshTokenGrant authRequest = new GoogleRefreshTokenGrant(httpTransport, jsonFactory, clientId, clientSecret, refreshToken);
authRequest.useBasicAuthorization = false;
AccessTokenResponse authResponse = authRequest.execute();
GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(authResponse.accessToken, httpTransport, jsonFactory, clientId, clientSecret, authResponse.refreshToken);
HttpRequestFactory rf = httpTransport.createRequestFactory(access);
String endPointUrl = "https://www.googleapis.com/tasks/v1/users/@me/lists";
GenericUrl endPoint = new GenericUrl(endPointUrl);
String requestBody = "{"maxResults":10000}";
HttpRequest request = rf.buildPostRequest(endPoint, ByteArrayContent.fromString("application/json", requestBody));
HttpResponse response = request.execute();
String str = response.parseAsString();
utils.log(str);
以下是我的回应:
{
"kind": "tasks#taskList",
"id": "MDA4NTI1NjMwNTAxMTQ5ODQ0NzM6OTA1NjU0MjI6MA",
"etag": ""M3V2EYzE8ZKSrA5JDxxyFB0Dbp4/zpmuA0Fyh_wtIkaqHMXDWlYqzd8"",
"title": "",
"updated": "2012-09-17T05:38:05.000Z",
"selfLink": "https://www.googleapis.com/tasks/v1/users/@me/lists/MDA4NTI1NjMwNTAxMTQ5ODQ0NzM6OTA1NjU0MjI6MA"
}
我什至尝试过requestBody
空的。但是没有用。
我做错了什么?
那是
我的错!
我没有使用GETRequest
方法,而是使用了POSTReques
t方法。
更改了以下行:
HttpRequest request = rf.buildPostRequest(endPoint, ByteArrayContent.fromString("application/json", requestBody));
自
HttpRequest request = rf.buildGetRequest(endPoint);
而且它工作正常!