如何让Google云端硬盘Java SDK读取/写入“我的云端硬盘”而不是别的地方? [英] How to make the Google Drive Java SDK read from/write to "my Drive" and not somewhere else?

查看:241
本文介绍了如何让Google云端硬盘Java SDK读取/写入“我的云端硬盘”而不是别的地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Google Drive(1.9.0 rev 155)使用最新的Java SDK,并且设法使其能够上传文件,列出它们,创建目录(这非常困难)以及其他各种各样的事情。 但是我上传的内容在Web界面中是不可见的,同样,我的代码也看不到Web界面中的内容。代码如下:

  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);
尝试{
SECRETS_FILE = clientSecrets.toRealPath();
} catch(IOException e){
throw new ExceptionInInitializerError(e);


$ b $ private main()
{
throw new Error(nice try!);

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

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

最终Drive drive =新的Drive.Builder(传输,JSON_FACTORY,
凭证).setApplicationName(APPLICATION_NAME).build();

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

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

final FileList fileList = list.execute();

最终列表< File> items = fileList.getItems();

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

}
}

好的,所以,从开发者控制台,我使用的是服务帐户凭据文件。还有Web应用程序的客户端ID。在开发者网站(以及SO问题/答案)的许多阅读之后,我仍然无法弄清楚两者之间的差异,仍然无法弄清楚如何修改在Web UI上的文件。



那么,我该怎么办? 服务帐户是 NOT 你呢。这是它自己的实体将其视为Google云端硬盘上的用户。它没有网络版Google驱动器,因为它不是真正的人,所以没有登录。它有自己的驱动器帐户,Google日历和其他一些可能的东西。如果你想能够看到它的文件,我认为有几个选项。



第一个选项上传到您自己的帐户。



转到您的Google云端硬盘网络用户界面。创建一个目录。采取服务帐户的电子邮件地址,并给它像任何其他用户一样访问。然后运行服务帐户。如果它有效,它应该有权访问这个新目录,然后您可以上传到该目录。



第二个选项。

让服务帐户让您访问其目录结构。可能使用权限:插入,方法是带上您的电子邮件地址并将其存取服务帐户目录。



笔记我没有在驱动器上测试过这个功能,但我已将它与Google Analytics和Google日历一起使用。我可能会尝试它只是为了它的乐趣。另外我不是Java程序员,所以无法真正帮助它实现它。我真的不认为这是一个Java问题,它更像是一个服务帐户设置问题:)

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 :)

这篇关于如何让Google云端硬盘Java SDK读取/写入“我的云端硬盘”而不是别的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆