我想使用Google Drive API for Android列出用户云端硬盘的Root文件夹中的所有文件.
我尝试过的:
使用SCOPE_FILE
和listChildren
:
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() { @Override public void onResult(DriveApi.MetadataBufferResult result) { if (!result.getStatus().isSuccess()) { Log.v("DKDK", "Request failed"); return; } MetadataBuffer metadataBuffer = result.getMetadataBuffer(); // empty! } }
我发现是SCOPE_FILE
只列出由应用程序,并不是所有的文件被创建的文件.
使用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.(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.
这是来自docs页面:
注意:Android Drive API仅适用于 https://www.googleapis.com/auth/drive.file范围.这意味着只有用户使用您的应用程序打开或创建的文件才能与查询匹配.
https://developers.google.com/drive/android/queries