0

看来,最近发布的 Google Drive SDK 支持目录列表。

https://developers.google.com/drive/v2/reference/files/list

我尝试将其集成到我的 Android 应用程序中。

package com.jstock.cloud;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.services.GoogleKeyInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.extensions.android2.AndroidHttp;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import com.jstock.gui.Utils;

public class CloudFile {
    public final java.io.File file;
    public final long checksum;
    public final long date;
    public final int version;
    private CloudFile(java.io.File file, long checksum, long date, int version) {
        this.file = file;
        this.checksum = checksum;
        this.date = date;
        this.version = version;
    }

    public static CloudFile newInstance(java.io.File file, long checksum, long date, int version) {
        return new CloudFile(file, checksum, date, version);
    }

    public static CloudFile loadFromGoogleDrive(String authToken) {
        final HttpTransport transport = AndroidHttp.newCompatibleTransport();
        final JsonFactory jsonFactory = new GsonFactory();
        GoogleCredential credential = new GoogleCredential();
        credential.setAccessToken(authToken);
        Drive service = new Drive.Builder(transport, jsonFactory, credential)
            .setApplicationName(Utils.getApplicationName())
            .setJsonHttpRequestInitializer(new GoogleKeyInitializer(ClientCredentials.KEY))
            .build();

        List<File> files = retrieveAllFiles(service);
        Log.i("CHEOK", "size is " + files.size());
        for (File file : files) {
            Log.i(TAG, "title = " + file.getTitle());
        }
        return null;
    }

    /**
     * Retrieve a list of File resources.
     *
     * @param service Drive API service instance.
     * @return List of File resources.
     */
    private static List<File> retrieveAllFiles(Drive service) {
        List<File> result = new ArrayList<File>();
        Files.List request = null;
        try {
            request = service.files().list();
        } catch (IOException e) {
            Log.e(TAG, "", e);
            return result;
        }

        do {
            try {
                FileList files = request.execute();

                result.addAll(files.getItems());
                request.setPageToken(files.getNextPageToken());
            } catch (IOException e) {
                Log.e(TAG, "", e);
                request.setPageToken(null);
            }
        } while (request.getPageToken() != null && request.getPageToken().length() > 0);

        Log.i("CHEOK", "yup!");
        return result;
    } 

    private static final String TAG = "CloudFile";
}

我总是从服务器返回 0 文件,并且没有抛出任何异常。有什么我错过的吗?

4

1 回答 1

2

您必须请求访问完整的云端硬盘范围才能列出所有文件:https ://developers.google.com/drive/scopes#requesting_full_drive_scope_for_an_app

如果您使用默认(和推荐)范围https://www.googleapis.com/auth/drive.file,您将只能看到应用程序创建或打开的文件。

于 2012-06-30T00:18:02.317 回答