如何使用 Google Drive REST AP 和 HTTPClient 创建文件夹?

问题描述:

我正在使用带有 HTTPClient 的 Google Drive REST API 创建一个文件夹.REST API 是文档此处 请注意,RESTAP 请求执行——但使用了错误的数据:tt 创建了一个名为untitled"的新文件并将我的 json 放在那里.

I am using Google Drive REST API with HTTPClient to create a folder. The REST API is document here Please note that the REST AP Request executes -- but uses the wrong data: tt creates a new file called "untitled" and puts my json there.

我尝试了使用 HTTPClient 构建 POST 请求并成功执行的不同方法——但 Google Drive 以某种方式响应创建文件并返回此数据并忽略我提交的 POST 数据.

I have tried different methods of building the POST request with HTTPClient which execute successfully -- but somehow the Google Drive responds with creating a file and returns this data and ignores POST data that I submit.

这是服务器的回复
..

This is what the server reponds with
..

"kind": "驱动器#文件","id": "0B-fYJN-c4UDjUlh1TUZadF9vejA",这是一个有效的 ID"title": "Untitled",----标题错误"mimeType": "application/json; charset=UTF-8", -- 这是错误的 mimeType..

"kind": "drive#file", "id": "0B-fYJN-c4UDjUlh1TUZadF9vejA", this is a valid ID "title": "Untitled", ----wrong title "mimeType": "application/json; charset=UTF-8", -- this is the wrong mimeType ..

我尝试了以下调用 API 的方法:我不确定我通过帖子发送的数据发生了什么.

I have tried the following ways of invoking the API : I am not sure what is happening to the data I am sending with the post.

1) 使用表单名称值对

1) Using form name value pairs

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("https://www.googleapis.com/upload/drive/v2/files?key="+GoogleClientConstants.GOOGLE_api_key);

postRequest.addHeader("Authorization", "Bearer " + accessToken);
postRequest.addHeader("Content-Type", "application/json; charset=UTF-8");


List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("title", "vip"));
nvps.add(new BasicNameValuePair("mimeType", "application/vnd.google-apps.folder"));
postRequest.setEntity(new UrlEncodedFormEntity(nvps, org.apache.http.Consts.UTF_8 ));
HttpResponse response = httpClient.execute(postRequest);

2) 使用 StringEntity

2) Using StringEntity

JSONObject jo= new JSONObject();
jo.element("title", "pets").element("mimeType", "application/vnd.google-apps.folder");



ContentType contentType= ContentType.create("application/json", Charset.forName("UTF-8")) ;
StringEntity entity = new StringEntity(jo.toString(), contentType); 

logger.info(" sending "+ jo.toString());


postRequest.setEntity(entity);


HttpResponse response = httpClient.execute(postRequest);

在这两种情况下,Google API 都以 200 和上述返回的 FileResource 响应.

In both cases , Google API responds with 200 and the above mentioned returned FileResource.

我已经使用您的代码创建了一个小示例.对我来说一切正常.请看我的源代码:

I've created a little example, using your code. And for me everything works OK. Please, see my source code:

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.JsonObject;

public class SourceCodeProgram {

    public static void main(String argv[]) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(
                "https://www.googleapis.com/drive/v2/files");
        post.addHeader("Content-Type", "application/json");
        post.addHeader("Authorization",
                "Bearer XXXXXXXXXXXXXXXXXXXXXXXXX");

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("title", "Test folder");
        jsonObject
                .addProperty("mimeType", "application/vnd.google-apps.folder");

        post.setEntity(new StringEntity(jsonObject.toString()));
        httpClient.execute(post);
    }
}

以上代码不会抛出任何异常.我检查了我的驱动器,我可以在其中看到测试文件夹".您在帖子 URL 中输入的密钥是什么?

Above code doesn't throw any exception. I've checked my Drive and I can see 'Test folder' in it. What the key are you putting in post URL?

您为什么不在您的应用中使用 Google 云端硬盘客户端库?

Why are you not using Google Drive Client Library in your app?