Fil*_*osa 2 java google-api google-api-java-client google-drive-api
我在使用 Google Drive API 上传文件时遇到了一些问题。API 不会返回任何错误并显示文件已成功上传。但是,当我尝试检索文件列表时,它不会出现,当我尝试查看 Google Drive API 页面时也不会出现。
以下是一些观察结果:
这是我的代码的外观(我尝试根据 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 的字符串”
{}
(其中 {} 应该是驱动器上存在的所有文件)
任何人都知道为什么它没有出现在驱动器上?
您似乎正在使用服务帐户进行身份验证。您需要记住服务帐户不是您,将其视为拥有自己的 Google 驱动器帐户的虚拟用户。您一直在将文件上传到服务帐户 Google Drive 帐户。你可以做一个 files.list 来查看这些上传的文件。服务帐户没有 Web 界面视图。
您可以使用服务帐户电子邮件地址并在您的个人谷歌驱动器帐户上共享一个目录,这将授予服务帐户上传到此目录的权限。您需要记住让服务帐户修补文件以授予您的用户访问文件的权限,否则您将没有任何权限。
| 归档时间: |
|
| 查看次数: |
1983 次 |
| 最近记录: |