问题
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.
回答1:
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?
回答2:
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
来源:https://stackoverflow.com/questions/15975674/how-to-create-a-folder-with-google-drive-rest-ap-and-httpclient