简体   繁体   中英

How to make the Google Drive Java SDK read from/write to “my Drive” and not somewhere else?

I'm using the latest Java SDK for Google Drive (1.9.0 rev 155) and I have managed to make it work to upload files, list them, create directories (which is suprisingly hard), and other various things.

But the content I upload is invisible in the Web interface, and similarly the content in the Web interface is invisible to my code. The code is as such:

public final class Main
{
    private static final String APPLICATION_NAME = "java7-fs-gdrive";
    private static final JsonFactory JSON_FACTORY
        = JacksonFactory.getDefaultInstance();

    private static final Path SECRETS_FILE;

    static {
        final String home = System.getProperty("user.home");
        if (home == null)
            throw new ExceptionInInitializerError("user.home not defined???");
        final Path clientSecrets
            = Paths.get(home, ".gdrive/clientSecrets.json");
        try {
            SECRETS_FILE = clientSecrets.toRealPath();
        } catch (IOException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    private Main()
    {
        throw new Error("nice try!");
    }

    public static void main(final String... args)
        throws GeneralSecurityException, IOException
    {
        final NetHttpTransport transport
            = GoogleNetHttpTransport.newTrustedTransport();

        final GoogleCredential credential;

        try (
            final InputStream in = Files.newInputStream(SECRETS_FILE);
        ) {
            credential = GoogleCredential.fromStream(in, transport,
                JSON_FACTORY).createScoped(ImmutableList.of(DriveScopes.DRIVE));
        }

        final Drive drive = new Drive.Builder(transport, JSON_FACTORY,
            credential).setApplicationName(APPLICATION_NAME).build();

        final Drive.Files files = drive.files();

        final Drive.Files.List list = files.list();

        final FileList fileList = list.execute();

        final List<File> items = fileList.getItems();

        for (final File item: items) {
            System.out.println(item.getTitle());
            System.out.println(item.getId());
            System.out.println(item.getKind());
        }

    }
}

OK, so, from the developer console, what I use is a "service account" credentials file. There are also "client ID for web applications". And after many reading trips to the developer site (and SO questions/answers) I still cannot figure out the difference between the two and still cannot figure out how to modify the files "on the web UI".

So, what should I do?

A service account is NOT you. It is its own entity think of it as a user on Google Drive. It doesn't have a Web version of Google drive because well its not really a person so doesn't have a log in. It does have its own drive account, Google Calendar and probably a few other things. If you want to be able to see the files it has I think there are a few options.

First option upload to your own account.

Go to your Google Drive web UI. Create a directory. take the service accounts email address and give it access like you would any other user. Then run the service account. If it works it should have access to this new directory then you can upload to that.

Second option.

Have the service account give you access to its directory structure. probably using Permissions: insert by taking your email address and giving it access to the service accounts directory.

note I haven't tested this with drive but I have used this with Google Analytics and Google Calendar. I might try it just for the fun of it. Also I am not a Java programmer so cant really help with getting it to work. I don't really think this is a Java problem its more of a Service account set up issue :)

How to make the Google Drive Java SDK read from/write to “my Drive”

As @DaImTo points out, a "service account" is NOT "my drive". Although the suggested sharing workaround might work, it is (imho) clumsy and hard to maintain.

If you want your app to be able to access "my Drive" directly, then it simply needs to

  1. acquire & store a refresh token for the "my drive" Google account,
  2. use that refresh token to fetch an access token whenever you require access,
  3. and finally use that access token in the usual way as a header or URL parameter to the Drive API.

Step 1 is outlined here How do I authorise an app (web or installed) without user intervention? (canonical ?)

Step 2 is standard Google Oauth, described https://developers.google.com/accounts/docs/OAuth2WebServer#refresh

Step 3 is standard Drive, eg https://developers.google.com/drive/v2/reference/files/get#try-it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM