java - 如何使用 Google Drive REST API 和 HTTPClient 创建文件夹?

标签 java google-api google-drive-api apache-httpclient-4.x

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

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

这是服务器响应的内容
..

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

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

1) 使用表单名值对

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



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

以上代码没有抛出任何异常。我检查了我的云端硬盘,我可以在其中看到“测试文件夹”。您在帖子 URL 中输入的 key 是什么?

你为什么不使用 Google Drive Client Library在您的应用中?

关于java - 如何使用 Google Drive REST API 和 HTTPClient 创建文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15975674/

相关文章:

java - 从 1.8 迁移到 openJDK11 时如何解决 "sun.security.x509"不可见问题?

android - 连接到 Google API 客户端时出现问题

android - Google Places Api 按距离排序

google-drive-api - Google Drive SDK : using drive. 安装范围将网络应用程序集成到 "open with"UI 不工作

php - 使用纯 PHP 在现有的 Google 电子表格中插入行

java - 如何使 JUnit 接受任何 Lambda 表达式

java - hibernate 异常 : Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

java - 如何根据每个字符串的不同部分对字符串数组进行排序?

google-app-engine - 限制我的 Google App Engine 应用程序访问 Google API key

Node.js Google Drive api 下载文件错误