如何使用Google Drive上的android上传/下载文件?

时间:2019-02-11 09:06:02

标签: android

我正在使用Google Drive Rest API在android上执行上载/下载操作,但是对于google doc上的可恢复操作没有明确的API文档。我已经成功获取驱动器操作的访问令牌。即使我不能添加google drive rest API标签。任何帮助将不胜感激。

我已经成功获取驱动器操作的访问令牌。任何帮助将不胜感激。

        try {
            URL url;
            url = new URL("https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable");
            HttpURLConnection request;
            request = (HttpURLConnection) url.openConnection();
            request.setRequestMethod("POST");
            request.setDoInput(true);
            request.setDoOutput(true);
            request.setRequestProperty("Authorization", "Bearer " + credential.getToken());
            request.setRequestProperty("X-Upload-Content-Type", getMimeType(file.getPath()));
            request.setRequestProperty("X-Upload-Content-Length", String.format(Locale.ENGLISH, "%d", file.length()));
            request.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            String body = "{\"name\": \"" + file.getName() + "\", \"parents\": [\"" + "parentId12345678" + "\"]}";
            request.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", body.getBytes().length));
            OutputStream outputStream ;
            outputStream = request.getOutputStream();
            outputStream.write(body.getBytes());
            outputStream.close();
            request.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }


1 个答案:

答案 0 :(得分:0)

读取Google drive rest API doc设置

  

https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable

会做

来自文档:

以下示例显示了如何发起与之的可恢复会话

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable HTTP/1.1
Authorization: Bearer [YOUR_AUTH_TOKEN]
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: image/jpeg
X-Upload-Content-Length: 2000000

{
  "name": "myObject"
}