[英]How to upload/download files using android on google drive?
I am using Google drive rest API to perform upload/download operations on android, but there is no clear API documentation for resumable operations on the google doc. 我正在使用Google Drive Rest API在android上执行上载/下载操作,但是在google doc上没有可恢复操作的明确API文档。 I have successfully get the access token for drive operations.
我已经成功获取驱动器操作的访问令牌。 Even I cannot add google drive rest API tag.
即使我不能添加google drive rest API标签。 Any help would be appreciated.
任何帮助,将不胜感激。
I have successfully get the access token for drive operations. 我已经成功获取驱动器操作的访问令牌。 Any help would be appreciated.
任何帮助,将不胜感激。
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();
}
Reading the Google drive rest API doc setting the 阅读Google Drive Rest API文档设置
https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable
https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable
will do the job 会做的工作
from the doc: 从文档:
The following example shows how to initiate a resumable session to 以下示例显示了如何启动一个可恢复会话来
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"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.