[英]How to force download of file from Google Team Drive using Google Drive API libraries in Java
在我的應用程序中,當用戶單擊我的應用程序中表單上的鏈接時,我試圖從Google Team Drive強制下載文件。
一些更新:
我有可以上傳到Team Drive以及在Team Drive上創建文件夾的代碼。
“團隊驅動器支持-此應用程序可與團隊驅動器中的文件正常工作”。 無法啟用Google Drive API設置上的-我可以選中此框,但由於總是禁用該按鈕,因此無法單擊“保存更改”按鈕。 但是,我認為這不是問題,因為我可以在代碼中與Team Drive進行交互。
我已閱讀了非常有限的Google文檔:
https://developers.google.com/drive/v3/web/manage-downloads
https://developers.google.com/drive/v3/reference/files/get
我還閱讀了一些帖子:
但是,我找不到有效的答案。 我建立了一些使用服務帳戶的代碼。 該代碼設置驅動器服務並將服務帳戶用作憑據。 我基於此Google文檔示例中的代碼下載部分:
https://developers.google.com/drive/v3/web/manage-downloads#examples
這是我正在使用的代碼:
public static void downloadFile(String fileID) {
// Set the drive service...
Drive service = null;
try {
service = getDriveService();
} catch (IOException e) {
e.printStackTrace();
}
OutputStream outputStream = new ByteArrayOutputStream();
try {
service.files().get(fileID).executeMediaAndDownloadTo(outputStream);
// NEED CODE HERE???????
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Build and return an authorized Drive client service.
*
* @return an authorized Drive client service
* @throws IOException
*/
public static Drive getDriveService() throws IOException {
Logger.info("GoogleDrive: getDriveService: Starting...");
GoogleCredential credential = null;
Drive googleDrive = null;
try {
credential = authorize();
Logger.info("GoogleDrive: getDriveService: Credentials set...");
googleDrive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
} catch (IOException ex) {
System.out.println(ex.toString());
ex.printStackTrace();
}
return googleDrive;
}
/**
* Creates an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
@SuppressWarnings("deprecation")
public static GoogleCredential authorize() throws IOException {
GoogleCredential credential = null;
String credentialsFileName = "";
try {
Logger.info("GoogleDrive: authorize: Starting...");
Logger.info("GoogleDrive: authorize: Getting credentialsFileName path...");
credentialsFileName = Configuration.root().getString("google.drive.credentials.file");
Logger.info("GoogleDrive: authorize: credentialsFileName = " + credentialsFileName);
Logger.info("GoogleDrive: authorize: Setting InputStream...");
InputStream in = GoogleDrive.class.getClassLoader().getResourceAsStream(credentialsFileName);
if (in == null) {
Logger.info("GoogleDrive: authorize: InputStream is null");
}
Logger.info("GoogleDrive: authorize: InputStream set...");
Logger.info("GoogleDrive: authorize: Setting credential...");
credential = GoogleCredential.fromStream(in, HTTP_TRANSPORT, JSON_FACTORY)
.createScoped(Collections.singleton(DriveScopes.DRIVE));
} catch (IOException ex) {
System.out.println(ex.toString());
System.out.println("Could not find file " + credentialsFileName);
ex.printStackTrace();
}
Logger.info("GoogleDrive: authorize: Ending...");
return credential;
}
當我的代碼運行時,沒有錯誤,也沒有任何反應。 我猜我在downloadFile函數中缺少一些代碼。 如果我在編碼中遺漏了一些東西或不正確,請隨時提供示例或我應該使用的正確代碼。
感謝您的幫助。
該演示不使用服務帳戶。
我將演示如何下載Team驅動器文件,並希望它可以使您對項目有深入的了解。
該代碼的基礎來自Drive API Java Quickstart :
public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
//this is what you're looking for - a way to download a file using 'webContentLink'
try {
Desktop desktop = java.awt.Desktop.getDesktop();
//place your webContentLink in the oURL variable
URI oURL = new URI("https://drive.google.com/a/google.com/uc?id=YOUR_FILE_ID&export=download");
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}
}
執行后,該程序將打開一個空白瀏覽器窗口,並將文件下載到您的計算機。
我的方式產生的webContentLink
只是使用Files.get嘗試,它並確保我設置supportsTeamDrives
至true
,也設置fields
參數webContentLink
所以只返回。
當然,您始終可以通過編程方式使用Java files.get
來獲取webContentLink
但使用Try-it來獲取webContentLink
webContentLink
容易進行測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.