简体   繁体   中英

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

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.

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": "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 ..

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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