Google驱动器API无法列出文件(v3 Java)
我在Java中使用GoogleAPI,并有以下代码片段
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
import java.lang.Object;
import java.util.ArrayList;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
public class DriveQuickstart {
private static final String APPLICATION_NAME = "DriveQuickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
// * Global instance of the scopes required by this quickstart.
// * If modifying these scopes, delete your previously saved tokens/ folder.
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
private static final String CREDENTIALS_FILE_PATH = "C:/Quickstart/src/main/resources/client_secret.json";
// * Creates an authorized Credential object.
// * @param HTTP_TRANSPORT The network HTTP Transport.
// * @return An authorized Credential object.
// * @throws IOException If the client_secret.json file cannot be found.
// */
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
public static void main(String[] args) throws IOException, GeneralSecurityException {
try
{
// Build a new authorized API client service.
System.out.println("This is a test line");
//declare final variable NetHttpTransport and GoogleNetHttpTransport from import statements
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
System.out.println("This is the second test line");
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
System.out.println("This is the third test line");
// Print the names and IDs for files.
FileList result = service.files().list()
.setPageSize(10)
.setFields("nextPageToken, files(id, name)")
.execute();
List<File> files = result.getFiles();
if (files == null || files.isEmpty()) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getName(), file.getId());
}
}
Drive.Files var1 = service.files();
System.out.println("var1 = " + var1.toString());
Drive.Files.List var2 = var1.list();
if (var2 == null)
System.out.println("null");
else {
System.out.printf("Var2 size is: %d\n", var2.size() );
}
//iterate through map and print results
}
catch(Exception e){
System.out.println("error");
}
System.out.println("This is the fourth test line");
}
}
}
更详细地说。我用了一种非传统的方法来尝试。我只使用命令行运行(没有IDE,没有gradle)
下面是一个指向ss的链接,其中包含我在命令提示符下的输出。 cmd output
假设我正在访问GoogleDrive中的元数据,并且该驱动器。文件嵌套在抽象映射(java.util.AbstractMap)中,我应该能够返回映射的大小,通过返回多少文件来了解它在哪里搜索
我不知道发生了什么事。第一次编译和运行时,我在chrome中打开了一个选项卡,请求获得访问元数据的权限以驱动程序。 第二次,它给出了“警告:无法更改每个人的权限:”错误。 最重要的是,var1('set to service.files()')返回com。谷歌。应用程序编程接口。服务。驾驶驱力$Files@458ad742使用toString()时。 子类服务。文件()。分配给var2的list()应该返回我的Google驱动器中的文件列表,但显然不是这样
我可能误解了很多东西,所以请容忍我。谢谢你的帮助
如果有人问,我已经在这里尝试了教程 https://developers.google.com/drive/api/v3/quickstart/java 它打开了标签,要求在我选择的谷歌账户上访问谷歌硬盘的元数据
因此,仅从这一点来看,我(认为)连接到了谷歌驱动。 但是,应该列出10个文件的名称和ID的默认代码没有任何作用
我如何运行该程序: 这是我用来编译和运行cmd代码的批处理文件
jar cfe DriveQuickstart.jar Quickstart DriveQuickstart.class
javac -cp ./* DriveQuickstart.java
java -cp ./* DriveQuickstart
出于某种原因,我不知道/不理解,这是唯一一种不会从不存在的导入jar文件返回方法的方法
# 1 楼答案
熟悉Java Quickstart以了解如何获得API调用的结果
问题
1-自动设置许可证
这似乎与this issue有关。这可能意味着用于从中运行代码的文件夹具有与预期不同的权限
如果您将代码放在桌面上(例如),然后从桌面上运行,是否会发生这种情况
2-
toString()
这个奇怪的字符串是Java打印类实例的方式
toString
方法打印出来的事实意味着这个方法没有被这个specific Class覆盖,所以它调用了通用的^{要打印出您希望看到的内容,请尝试嵌套方法