当前位置: 首页 > 知识库问答 >
问题:

使用google Drive API和Java读取共享驱动器

阎安邦
2023-03-14

我想列出所有可用的共享驱动器和每个共享驱动器文件和文件夹使用谷歌驱动器v3 api和java,下面的代码我写的,

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import s.DriveQuickstart;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

public class SharedDrive {
    private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    /**
     * Global instance of the scopes required by this quickstart.
     * If modifying these scopes, delete your previously saved tokens/ folder.
     */
    private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
    private static final String CREDENTIALS_FILE_PATH = "/h.json";

    /**
     * Creates an authorized Credential object.
     * @param HTTP_TRANSPORT The network HTTP Transport.
     * @return An authorized Credential object.
     * @throws IOException If the cc.json file cannot be found.
     */
    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = DriveQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("dummy@gmail.com");
    }

    public static void main(String... args) throws IOException, GeneralSecurityException {
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();
            service.teamdrives();
        // Print the names and IDs for up to 10 files.
        DriveList result = service.drives().list()
                .execute();
        List<com.google.api.services.drive.model.Drive> drives = result.getDrives();
        if (drives == null || drives.isEmpty()) {
            System.out.println("No files found.");
        }
    }
}

我得到下面的错误

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Insufficient Permission: Request had insufficient authentication scopes.",
    "reason" : "insufficientPermissions"
  } ],
  "message" : "Insufficient Permission: Request had insufficient authentication scopes."
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1045)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at SharedDrive.main(SharedDrive.java:69)

进程已完成,退出代码为1

暂时还没有答案

 类似资料:
  • 是否可以使用服务帐户创建共享驱动器?。 由于没有UI服务号创建文件/文件夹,我需要一些方法 /APIs支持创建共享驱动器。 我可以在服务号下创建孩子(文件和文件夹),但不确定如何使用服务号创建共享驱动器。 向你问好,索拉夫

  • 当我运行下面的代码时,它取自:https://developers.google.com/drive/api/v3/manage-shareddrives#python 我得到“用户对此文件没有足够的权限”如果我创建文件、列出共享驱动器或其他任何东西,都不会发生这种情况。除了['https://www.googleapis.com/auth/drive']. 需要注意的是,我使用的是服务号。服务帐

  • 问题内容: 我试图在共享内存上发布一些随机的东西;出于某些奇怪的原因,阅读器没有选择发件人写的东西 这是发件人: 这是读者: 如果重新启动阅读器,则阅读器确实能够读取发送方已写入的最后一个值。 但是,如果我先启动阅读器,然后再启动发送器,则阅读器不会拾取发送器写入的所有值。 为了使这个更奇怪,如果我在SHM :: read()中取消对printf语句的注释,那么读者有时可以使用。 任何的想法? G

  • 我正在尝试Google Drive API V3。 拥有一些共享驱动器文件夹的访问权限,并且是所有这些资源的管理员,我想获取一个通过查询筛选的特定文件夹。 出于任何原因,当我尝试按照留档建议插入查询时,它会响应一个错误,这个错误不是很描述性,也不利于调试: 此外,如果我不带条件地执行查询,则响应会成功,并显示整个共享驱动器: 有什么帮助吗? 谢谢

  • 我正试图创建一个共享驱动器使用下面的谷歌api和驱动器。创建方法。 但它给了我错误 “消息”:“用户对此文件没有足够的权限。” 我怎样才能做到这一点?