0

我尝试使用 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空。但是没有用。

我做错了什么?

4

1 回答 1

0

那是我的错!

GETRequest我用POSTRequest 方法代替了Method。

更改了以下行:

HttpRequest request = rf.buildPostRequest(endPoint, ByteArrayContent.fromString("application/json", requestBody));

HttpRequest request = rf.buildGetRequest(endPoint);

它工作正常!

于 2012-09-17T06:00:29.650 回答