3

Google ドライブ API を使用して、Google ドライブ内のファイルを一覧表示するプログラムを作成しようとしています。

アカウントのサービス アカウントを作成しました。

これが私が作成したサービスオブジェクトです

public static Drive getDriveService() throws GeneralSecurityException,
        IOException, URISyntaxException {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountScopes(
                    "https://www.googleapis.com/auth/drive")
            .setServiceAccountPrivateKeyFromP12File(
                    new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
            .build();
    Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
            .setApplicationName("FileListAccessProject")
            .setHttpRequestInitializer(credential).build();

    return service;
}

このサービス オブジェクトを使用して、file.list と呼びました

    private static List<File> retrieveAllFiles(Drive service)
        throws IOException {
    List<File> result = new ArrayList<File>();
    Files.List request = service.files().list();

    do {
        try {
            FileList files = request.execute();
            System.out.println(files);
            result.addAll(files.getItems());
            request.setPageToken(files.getNextPageToken());
        } catch (IOException e) {
            System.out.println("An error occurred: " + e);
            request.setPageToken(null);
        }
    } while (request.getPageToken() != null
            && request.getPageToken().length() > 0);
    // System.out.println(result);
    return result;
}

しかし、これは空のアイテム配列を返しています

{"etag":"\"8M2pZwE_fwroB5BIq5aUjc3uhqg/vyGp6PvFo4RvsFtPoIWeCReyIC8\"",
"kind":"drive#fileList","selfLink":"https://www.googleapis.com/drive/v2/files",
 "items":[]}

誰かがこれで私を助けてくれますか、私は何か不足していますか?

ありがとう。

4

1 に答える 1

4

通常のアカウントに似ていますが、ユーザーではなくアプリに属しているアプリケーション所有のアカウントを使用しています。

アプリケーション所有のアカウントには特別なアクセス許可がなく、通常のアカウントとして、自分が所有するドキュメントまたは共有されたドキュメントにのみアクセスできます。したがって、通常のアカウントがファイルを所有している場合、サービス アカウントはファイルを一覧表示できません。

ドメイン全体の委任を使用して、アプリが Google Apps ドメイン内の他のユーザーに代わってファイルにアクセスできるようにすることができます。

https://developers.google.com/drive/delegation

于 2013-03-08T05:11:42.717 に答える