How do I get the Java SDK Google Drive to read / write to "my disk" and not somewhere else?

I am using the newest Java SDK for Google Drive (1.9.0 rev 155) and I managed to get it to work to download files, list them, create directories (which is surprisingly difficult) and other various things.

But the content I am loading is not visible in the web interface, and similarly, the content in the web interface is invisible to my code. The code is the same:

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 I'm using the "service account" credential file. There is also a "client ID for web applications". And after many reading trips to the developer site (and questions and answers), I still can't figure out the difference between the two and still can't figure out how to change the files "in the web interface".

So what should I do?

+3


source to share


2 answers


Service account is NOT . His own self thinks of it as a user in Google Drive. It doesn't have a web version of Google Drive as it doesn't actually have a human, so it doesn't have a login. It has its own drive account, Google Calendar, and maybe a few other things. If you want to see the files that I have, I think there are several options.

The first option is uploading to your account.

Go to the Google Drive web interface. Create a directory. take the email address of the service accounts and give it access like any other user. Then start the service account. If it works, it should have access to this new directory, after which you can download it.



Second option.

Grant the service account 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 disk, but have used this with Google Analytics and Google Calendar. I could try it just for fun. Also, I'm not a Java programmer, so I can't help him get it to work. Actually I don't think this is a Java issue, the problem with the service account setup :)

+2


source


How to make Java SDK on Google Drive read / write to "My Drive"

As @DaImTo points out, "service account" is NOT "my drive". While the suggested workaround for sharing might work, it is (imho) awkward and awkward.

If you want your application to be able to access "my disk" directly, you just need to

  • get and save the refresh token for my drive google account,
  • use this refresh token to get access token when you need access,
  • and finally, use this access token in the usual way as a header or URL parameter for the drive API.


Step 1 is described here How can I allow an application (website or installed) without user intervention? (canonical?)

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

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

+1


source







All Articles