列出云端硬盘中的所有文件

List all files in Drive

我想使用 Google 驱动器 API 为 Android 列出用户驱动器根文件夹中的所有文件。

我尝试过的:

  1. 使用SCOPE_FILElistChildren

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    

然后列出文件:

Drive.DriveApi.getRootFolder(mGoogleApiClient)
    .listChildren(mGoogleApiClient)
    .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
        @Override
        public void onResult(DriveApi.MetadataBufferResult result) {
            if (!result.getStatus().isSuccess()) {
                Log.v("DKDK", "Request failed");
                return;
            }
            MetadataBuffer metadataBuffer = result.getMetadataBuffer(); // empty!
        }
    }

found SCOPE_FILE 只列出应用程序创建的文件,而不是所有文件。

  1. 使用https://www.googleapis.com/auth/drive范围

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API)
            .addScope(new Scope("https://www.googleapis.com/auth/drive"))
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    

异常失败:

com.google.android.gms.drive.auth.c: Authorization failed: Unsupported scope: https://www.googleapis.com/auth/drive
at com.google.android.gms.drive.auth.g.a(SourceFile:86)
at com.google.android.gms.drive.api.e.<init>(SourceFile:230)
at com.google.android.gms.drive.api.a.q.a(SourceFile:71)
at com.google.android.gms.common.service.f.run(SourceFile:176)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at com.google.android.gms.common.util.a.c.run(SourceFile:17)
at java.lang.Thread.run(Thread.java:856)

它是打算以这种方式工作,还是我遗漏了什么?

您无法使用 Google Drive API(Android 库)列出所有文件(不是由您的应用程序创建的文件)。您应该使用 Google Drive REST API.

您尝试使用的范围是 REST API。

这里来自文档页面:

Note: The Android Drive API only works with the https://www.googleapis.com/auth/drive.file scope. This means that only files which a user has opened or created with your application can be matched by a query.

https://developers.google.com/drive/android/queries