要从Google Drive API的Java控制台获取信息,您需要按照以下步骤操作:
在您的Java项目中,使用Maven或Gradle安装Google API客户端库。
在pom.xml
文件中添加以下依赖项:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
**version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifact|ersion>1.23.0</artifactId>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-drive</artifactId>
<version>v3-rev20200615-1.23.0</version>
</dependency>
在build.gradle
文件中添加以下依赖项:
implementation 'com.google.api-client:google-api-client:1.23.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
implementation 'com.google.apis:google-api-services-drive:v3-rev20200615-1.23.0'
创建一个Java类,例如DriveApiExample.java
,并编写以下代码:
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.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
public class DriveApiExample {
private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
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_READONLY);
private static final String CREDENTIALS_FILE_PATH = "/path/to/credentials.json";
public static void main(String[] args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
Drive service = new Drive.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JSON_FACTORY,
new AuthorizationCodeInstalledApp(
new GoogleAuthorizationCodeFlow.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JSON_FACTORY,
CREDENTIALS_FILE_PATH,
SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build(),
new LocalServerReceiver.Builder().setPort(8888).build())
.setApplicationName(APPLICATION_NAME)
.build());
// Print the names and IDs for up to 10 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("Found file: %s (%s)
", file.getName(), file.getId());
}
}
}
}
确保您的credentials.json
文件位于正确的路径(在上面的代码中,它被设置为/path/to/credentials.json
)。然后运行Java程序。程序将提示您授权访问Google Drive,然后显示您的文件列表
领取专属 10元无门槛券
手把手带您无忧上云