我正在使用最新的 Google Drive Java SDK(1.9.0 rev 155),并且我已经成功地使其能够上传文件、列出文件、创建目录(这非常困难)以及其他各种操作。
但是我上传的内容在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");
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());
}
}
}
好的,那么,在开发人员控制台中,我使用的是“服务帐户”凭据文件。还有“Web 应用程序的客户端 ID”。经过多次阅读开发者网站(以及 SO 问题/答案)后,我仍然无法弄清楚两者之间的区别,并且仍然无法弄清楚如何修改“在 Web UI 上”的文件。
所以我该怎么做?
服务帐户是NOT你。它是它自己的实体,将其视为 Google Drive 上的用户。它没有网络版的 Google Drive,因为它不是一个真正的人,所以没有登录权限。它有自己的 Drive 帐户、Google 日历,可能还有其他一些东西。如果您希望能够查看其中的文件,我认为有几个选项。
第一个选项上传到您自己的帐户。
转到您的 Google 云端硬盘网络用户界面。创建一个目录。获取服务帐户的电子邮件地址并像对待任何其他用户一样授予其访问权限。然后运行服务帐户。如果它有效,它应该可以访问这个新目录,然后您可以上传到该目录。
第二个选择。
让服务帐户允许您访问其目录结构。可能使用权限:插入 https://developers.google.com/drive/v2/reference/permissions/insert通过获取您的电子邮件地址并授予其访问服务帐户目录的权限。
note我还没有使用驱动器对此进行测试,但我已将其与 Google Analytics 和 Google Calendar 一起使用。我可能只是为了好玩而尝试一下。另外,我不是 Java 程序员,所以无法真正帮助它工作。我真的不认为这是一个 Java 问题,它更多的是一个服务帐户设置问题:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)