Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

android - List all files in Drive

I want to list all files in Root folder of user's Drive using Google Drive API for Android.

What I've tried:

  1. Use SCOPE_FILE and listChildren:

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

Then list files:

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!
        }
    }

I've found that SCOPE_FILE only list files that were created by the app, not all files.

  1. Use https://www.googleapis.com/auth/drive scope

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

Fails with exception:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

The scope that you are trying to use is for REST API.

Here is from docs page:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...