列出云端硬盘中的所有文件 [英] List all files in Drive
问题描述
我想使用Android版Google Drive API列出用户云端硬盘的Root文件夹中的所有文件.
I want to list all files in Root folder of user's Drive using Google Drive API for Android.
我尝试过的事情:
-
使用
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<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
Log.v("DKDK", "Request failed");
return;
}
MetadataBuffer metadataBuffer = result.getMetadataBuffer(); // empty!
}
}
已找到 SCOPE_FILE
仅列出应用程序创建的文件,而不是所有文件.
I've found that SCOPE_FILE
only list files that were created by the app, not all files.
-
使用
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)
它打算以此方式工作,还是我丢失了某些东西?
Is it intended to work this way, or I'm missing something?
推荐答案
您无法使用Google Drive API(Android库)列出所有文件(不是由您的应用创建的文件).您应该使用Google Drive REST API.
You can't list all files (files that wasn't created by your app) with Google Drive API (Android library). You should use Google Drive REST API.
您要使用的范围是针对REST API.
The scope that you are trying to use is for REST API.
这是来自文档页面:
注意:Android Drive API仅适用于 https://www.googleapis.com/auth/drive.file 作用域.这意味着只有 用户使用您的应用程序打开或创建的文件可以是 与查询匹配.
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
这篇关于列出云端硬盘中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!