我正在尝试使用以下代码
使用V3 rest api从谷歌驱动器下载文件public static void downloadFileAsChunksGdrive() throws IOException {
String accessToken = "XYZ";
Credential credential = new GoogleCredential().setAccessToken(accessToken);
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("ABC")
.build();
File file = service.files().get("fileId").setFields("size, webContentLink").execute();
String downloadUrl = file.getWebContentLink();
System.out.println(downloadUrl);
long fileSize = file.getSize();
System.out.println(fileSize);
java.io.File outputFile = new java.io.File("/A/B/C/"+ file.getName())
FileOutputStream fos = new FileOutputStream(outputFile);
for (long i = 0; i< fileSize; i=i+1000000) {
byte[] fileRangeBytes = getBytes(service, downloadUrl, i, 1000000);
fos.write(fileRangeBytes);
fos.flush();
}
fos.close();
}
private static byte[] getBytes(Drive drive, String downloadUrl, long position, long byteCount) {
byte[] receivedByteArray = null;
if (downloadUrl != null && downloadUrl.length() > 0) {
try {
com.google.api.client.http.HttpRequest httpRequestGet = drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl));
httpRequestGet.getHeaders().setRange("bytes=" + position + "-" + (position + byteCount - 1));
com.google.api.client.http.HttpResponse response = httpRequestGet.execute();
InputStream is = response.getContent();
receivedByteArray = IOUtils.toByteArray(is);
response.disconnect();
System.out.println("google-http-client-1.18.0-rc response: [" + position + ", " + (position + receivedByteArray.length - 1) + "]");
} catch (IOException e) {
e.printStackTrace();
}
}
return receivedByteArray;
}
执行代码时,我收到以下错误
com.google.api.client.http.HttpResponseException: 302 Moved Temporarily
<HTML>
<HEAD>
<TITLE>Moved Temporarily</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>
The document has moved <A HREF="https://doc-00-a0-docs.googleusercontent.com/docs/securesc/pcngq5lgspj8i9l0m7amt8h5ad9otomr/6pm24trt0vnj4ksogqjedtkfhhh7588a/1495101600000/06178136502342984870/06178136502342984870/0B08iWIDcqPjkR2VCNFNqYXlBN0E?e=download&nonce=8acfbud35mah2&user=06178136502342984870&hash=4iv88ck5iea3ql8pbfv5gje54ufr2h8h">here</A>.
</BODY>
</HTML>
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1061)
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
这是因为该页面正在尝试重定向到用户身份验证页面。
我通过使用带有'alt = media'查询参数的文件获取请求并且工作代码在下面
来实现它private static byte[] getBytes(Drive drive, String accessToken, String fileId, long position, long byteCount) {
byte[] receivedByteArray = null;
String downloadUrl = "https://www.googleapis.com/drive/v3/files/" + fileId + "?alt=media&access_token="
+ accessToken;
if (downloadUrl != null && downloadUrl.length() > 0) {
try {
com.google.api.client.http.HttpRequest httpRequestGet = drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl));
httpRequestGet.getHeaders().setRange("bytes=" + position + "-" + (position + byteCount - 1));
com.google.api.client.http.HttpResponse response = httpRequestGet.execute();
InputStream is = response.getContent();
receivedByteArray = IOUtils.toByteArray(is);
response.disconnect();
System.out.println("google-http-client-1.18.0-rc response: [" + position + ", " + (position + receivedByteArray.length - 1) + "]");
} catch (IOException e) {
e.printStackTrace();
}
}
return receivedByteArray;
}