简体   繁体   English

带有正文内容的Google API Java GET请求

[英]Google API Java GET request with body content

My Goal is to request GoogleTaskAPI for TASKLIST with specified no.of result. 我的目标是请求带有指定结果编号的TASKLIST的GoogleTaskAPI。 It works fine, If I m passing no requestBody . 如果我没有传递requestBody ,它工作正常。 But I need to pass request parameter to specific number of results to be returned. 但是我需要将请求参数传递给要返回的特定数量的结果。 When I do that, it creates new Tasklist, Instead of listing. 当我这样做时,它将创建新的任务列表,而不是列表。 So how to do this? 那么该怎么做呢?

My Code: 我的代码:

    GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(accessToken, httpTransport, jsonFactory, clientId, clientSecret, refreshToken);
    HttpRequestFactory rf = httpTransport.createRequestFactory(access);

    String endPointUrl = "https://www.googleapis.com/tasks/v1/users/@me/lists";
    String requestBody = "{\"maxResults\":3}";

    GenericUrl endPoint = new GenericUrl(endPointUrl);
    ByteArrayContent content = new ByteArrayContent("application/json", requestBody.getBytes());

    //Try 0: Works, But Retrieving all of my Tasklist, I need only 3
    //HttpRequest request = rf.buildGetRequest(endPoint);
    //-------

    //Try 1: Fails to retrieve
    //HttpRequest request = rf.buildGetRequest(endPoint);
    //request.setContent(content);
    //request.getContent().writeTo(System.out);
    //-------

    //Try 2: Fails to retrieve
    HttpRequest request = rf.buildRequest(HttpMethod.GET, endPoint, content);
    request.getContent().writeTo(System.out);
    //-------

    HttpResponse response = request.execute();
    String str = response.parseAsString();
    utils.log(str);  

maxResults is a query parameter, not a request parameter, so you can just put it in the url: maxResults是查询参数,而不是请求参数,因此您可以将其放在url中:

String endPointUrl = "https://www.googleapis.com/tasks/v1/users/@me/lists?maxResults=3";

You should also consider using the Java client's Tasks interface for making requests; 您还应该考虑使用Java客户端的“任务”接口进行请求。 it may be a little easier since it handles the details of the url for you: 这可能会更容易一些,因为它可以为您处理url的详细信息:

http://code.google.com/p/google-api-java-client/wiki/APIs#Tasks_API http://code.google.com/p/google-api-java-client/wiki/APIs#Tasks_API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM