我正在使用Java中的Google API,并且具有以下代码段。
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,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
假设我实际上正在访问Google云端硬盘中的元数据,并且Drive.Files嵌套在一个抽象地图(java.util.AbstractMap)内,我应该能够返回地图的大小以了解一个想法按返回多少文件进行搜索的位置。
我不确定发生了什么。第一次编译并运行时,我在chrome中打开了标签页,要求获得访问权限以获取对要驱动的元数据的访问权限。 第二次,它显示“警告:无法更改每个人的权限:”错误。 最重要的是,使用toString()时,var1(“设置为service.files()”)返回com.google.api.services.drive.Drive$Files@458ad742。 分配给var2的子类service.files()。list()应该返回我的Google云端硬盘中的文件列表,但显然并没有这样做。
我可能会误会很多东西,所以请多多包涵。感谢您的帮助。
万一有人问,我已经在这里尝试了本教程 https://developers.google.com/drive/api/v3/quickstart/java 它打开了标签,要求我选择的任意一个Google帐户上的元数据访问Google云端硬盘。
仅此一项,我(认为)已连接到Google云端硬盘。 但是,应该列出10个文件的名称和ID的默认代码无法执行任何操作。
我如何运行程序: 这是我用来在cmd中编译和运行代码的批处理文件。
jar cfe DriveQuickstart.jar Quickstart DriveQuickstart.class
javac -cp ./* DriveQuickstart.java
java -cp ./* DriveQuickstart
由于某种原因,我不知道/不了解,这是唯一一种不会从不存在的导入jar文件中返回方法的方法。