使用 Google Drive API 时不显示上传的文件

Fil*_*osa 2 java google-api google-api-java-client google-drive-api

我在使用 Google Drive API 上传文件时遇到了一些问题。API 不会返回任何错误并显示文件已成功上传。但是,当我尝试检索文件列表时,它不会出现,当我尝试查看 Google Drive API 页面时也不会出现。

以下是一些观察结果:

  • 我已经检查过该路径是否存在于 Java 上,并且确实存在。
  • 文件上传成功,返回一个ID,进度为1.0,没有消息错误。
  • 我使用的范围是驱动器全局范围,而不是文件范围。“ https://www.googleapis.com/auth/drive
  • 该查询成功显示在 Google 的控制台开发人员页面上。
  • 我已经尝试使用 root 作为文件夹而不是创建文件夹来创建它。
  • 本项目使用的 Api 版本为“v3-rev90-1.23.0”

这是我的代码的外观(我尝试根据 Java 代码段创建它)。

public class Quickstart {   
/** Application name. */
private static final String APPLICATION_NAME = "Drive API Java Quickstart";
/** Directory to store user credentials for this application. */

/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;

/**
 * Global instance of the scopes required by this quickstart.
 *
 * If modifying these scopes, delete your previously saved credentials at
 * ~/.credentials/drive-java-quickstart
 */
private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE);

static {
    try {
        HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}

/**
 * Creates an authorized Credential object.
 * 
 * @return an authorized Credential object.
 * @throws IOException
 */
public static Credential authorize() throws IOException {
    GoogleCredential credential = GoogleCredential.fromStream(Quickstart.class.getResourceAsStream("/secret.json"));
    credential = credential.createScoped(SCOPES);
    return credential;

}

/**
 * Build and return an authorized Drive client service.
 * 
 * @return an authorized Drive client service
 * @throws IOException
 */
public static Drive getDriveService() throws IOException {
    Credential credential = authorize();

    return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
}

public static String generateFolder(Drive service) throws IOException {
    File fileMetadata = new File();
    fileMetadata.setName("Invoices");
    fileMetadata.setMimeType("application/vnd.google-apps.folder");

    File file = service.files().create(fileMetadata).setFields("id").execute();
    System.out.println("Folder ID: " + file.getId());
    return file.getId();
}

public static void uploadFile(Create createFile, Drive service) throws IOException, InterruptedException {
    File file = createFile.execute();
    Thread.sleep(3000);
    MediaHttpUploader uploader = createFile.getMediaHttpUploader();
    System.out.println(uploader.getProgress());
    System.out.println("File ID: " + file.getId());
    System.out.println(service.files().list());
}

public static Create createFile(String path, Drive service) throws IOException {
    String folderId = generateFolder(service);
    File fileMetadata = new File();
    fileMetadata.setName("photo.jpg");
    fileMetadata.setParents(Collections.singletonList(folderId));
    java.io.File filePath = new java.io.File(path);
    FileContent mediaContent = new FileContent("image/jpeg", filePath);
    Create createFile = service.files().create(fileMetadata, mediaContent).setFields("id, parents");
    return createFile;
}

public static void main(String[] args) throws IOException, InterruptedException {
    Drive service = getDriveService();
    String path = "F:/workspace/drive/files/photo.jpg";
    Create createdFile = createFile(path, service);
    uploadFile(createdFile, service);
}

}
Run Code Online (Sandbox Code Playgroud)

此代码的结果是:
文件夹 ID:“文件夹 ID 的字符串”
1.0
文件 ID:“文件 ID 的字符串”
{}
(其中 {} 应该是驱动器上存在的所有文件)

任何人都知道为什么它没有出现在驱动器上?

DaI*_*mTo 7

您似乎正在使用服务帐户进行身份验证。您需要记住服务帐户不是您,将其视为拥有自己的 Google 驱动器帐户的虚拟用户。您一直在将文件上传到服务帐户 Google Drive 帐户。你可以做一个 files.list 来查看这些上传的文件。服务帐户没有 Web 界面视图。

您可以使用服务帐户电子邮件地址并在您的个人谷歌驱动器帐户上共享一个目录,这将授予服务帐户上传到此目录的权限。您需要记住让服务帐户修补文件以授予您的用户访问文件的权限,否则您将没有任何权限。