【问题标题】:How to force download of file from Google Team Drive using Google Drive API libraries in Java如何使用 Java 中的 Google Drive API 库从 Google Team Drive 强制下载文件
【发布时间】:2018-03-02 15:13:26
【问题描述】:

在我的应用程序中,当用户在我的应用程序中单击表单上的链接时,我试图强制从 Google Team Drive 下载文件。

一些更新:

  1. 我有可以上传到团队云端硬盘以及在团队云端硬盘上创建文件夹的代码。

  2. “团队云端硬盘支持 - 此应用程序可以正常处理团队云端硬盘中的文件。”无法启用 Google Drive API 设置 - 我可以选中此框,但无法单击“保存更改”按钮,因为该按钮始终处于禁用状态。但是,我认为这不是问题,因为我可以在代码中与 Team Drive 交互。

我已经阅读了谷歌文档,内容非常有限:

https://developers.google.com/drive/v3/web/manage-downloads

https://developers.google.com/drive/v3/reference/files/get

我也看过一些帖子:

Download files from google drive using java API

但是,我找不到有效的答案。我已经构建了一些使用服务帐户的代码。该代码设置驱动器服务并使用服务帐户作为凭据。我基于此 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 函数中缺少一些代码。如果我在编码中遗漏了某些内容或不正确,请随时提供示例或我应该使用的正确代码。

感谢您的帮助。

【问题讨论】:

    标签: java google-api google-drive-api


    【解决方案1】:

    此演示不使用服务帐户。

    我将演示如何下载团队驱动文件,希望它能让您深入了解您的项目。

    代码的基础取自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 Try-it 并确保我将supportsTeamDrives 设置为true,并将fields 参数设置为webContentLink,所以它只返回那个。

    当然,您始终可以使用 Java files.get 以编程方式获取 webContentLink,但使用 Try-it 获取 webContentLink 更容易用于测试目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 2023-02-06
      • 2018-10-14
      • 2015-02-06
      相关资源
      最近更新 更多