如何在谷歌服务帐户的驱动器中下载文件

时间:2017-12-21 14:49:17

标签: android google-drive-api service-accounts

  • 我可以使用com.google.api.services.drive 插入 /上传以及列表我的Google服务帐户中的文件。云端硬盘.files()。list()
  • 但是,当我使用 getDownloadUrl()下载文件时,我会在外部下载程序/浏览器中收到(无效的登录凭据)消息
  • 或者即使我通过我的应用程序下载,我也会收到java.io. FileNotFoundException

这是我的代码:

public void getDriveService() throws GeneralSecurityException, IOException, URISyntaxException {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    ArrayList<String> scopes = new ArrayList<String>();
    scopes.add(DriveScopes.DRIVE);
    scopes.add(DriveScopes.DRIVE_APPDATA);
    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId("akh....@smart-spark-13..5.iam.gserviceaccount.com")
            .setServiceAccountScopes(scopes)
            .setServiceAccountPrivateKeyFromP12File(File p12)
            .build();

    com.google.api.services.drive.Drive service = new com.google.api.services.drive.Drive.Builder(httpTransport, jsonFactory, null)
            .setHttpRequestInitializer(credential)
            .build();
    //return service;
    mydrive = service;
    }

这可能与文件权限有关,但我可以使用getThumbnailUrl()查看文件的缩略图链接,但是通过getDownloadUrl()下载会抛出异常;

0 个答案:

没有答案