如何使用Google Drive REST AP和HTTPClient创建文件夹? [英] How to create a Folder with Google Drive REST AP and HTTPClient?

查看:204
本文介绍了如何使用Google Drive REST AP和HTTPClient创建文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Drive REST API和HTTPClient创建文件夹。
REST API的文档此处请请注意,REST AP请求执行 - 但使用了错误的数据:tt创建了一个名为untitled的新文件,并将我的json放在那里。

我尝试了不同的方法使用HTTPClient构建POST请求,并成功执行 - 但不知何故,Google Drive响应创建文件并返回此数据,并忽略我提交的POST数据。



是什么服务器响应

..



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



我尝试了以下调用API的方式:我不确定发生了什么事与帖子一起发送。
$ b $ 1
使用表单名称值对

$ p $ 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

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


$ b 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。

解决方案

我用你的代码创建了一个小例子。对我而言,一切正常。请查看我的源代码:

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


以上代码不会引发任何异常。我已经检查了我的云端硬盘,并且可以看到其中的测试文件夹。你在网址中输入的关键是什么?



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


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?

这篇关于如何使用Google Drive REST AP和HTTPClient创建文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆