How to create a Folder with Google Drive REST AP and HTTPClient?

北战南征 提交于 2019-12-04 18:54:08

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);
    }
}

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?

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

To create a SUBfolder you have to use the ID(s) of the parent folder(s), for example to create the folder "sub" in a parent folder "super" that has ID XXX:

POST https://www.googleapis.com/drive/v2/files
Authorization: Bearer {ACCESS_TOKEN}
Content-Type: application/json
...
{
  "title": "sub",
  "parents": [{"id":"XXX"}]
  "mimeType": "application/vnd.google-apps.folder"
}

To create root folders, you can obmit the "parents" argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!