首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Google Drive SDK - Java示例无效

Google Drive SDK已经被弃用,现在建议使用Google Drive REST API。以下是使用Java和Google Drive REST API的一个简单示例,该示例展示了如何上传文件到Google Drive:

首先,确保你已经设置了Google Cloud项目并启用了Google Drive API。然后,安装Google API客户端库:

代码语言:javascript
复制
mvn install -DgroupId=com.google.apis -DartifactId=google-api-services-drive -Dversion=v3-rev20211108-2.0.0

接下来,创建一个名为DriveQuickstart的Java类,并将以下代码粘贴到该类中:

代码语言:javascript
复制
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collections;

public class DriveQuickstart {
    private static final String APPLICATION_NAME = "Google Drive REST API Java Quickstart";
    private static final String CREDENTIALS_FILE_PATH = "path/to/your/credentials.json";
    private static final String FILE_NAME = "example.txt";
    private static final String UPLOAD_FILE_PATH = "path/to/your/upload_file.txt";

    public static void main(String... args) throws IOException, GeneralSecurityException {
        // Build a new authorized API client service.
        final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        final JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        Drive service = new Drive.Builder(httpTransport, jsonFactory, getCredentials(httpTransport))
                .setApplicationName(APPLICATION_NAME)
                .build();

        // Upload a file to Google Drive
        File fileMetadata = new File();
        fileMetadata.setName(FILE_NAME);
        java.io.File filePath = new java.io.File(UPLOAD_FILE_PATH);
        FileInputStream inputStream = new FileInputStream(filePath);
        File file = service.files().create(fileMetadata, inputStream, filePath.getName()).execute();
        System.out.println("File ID: " + file.getId());
    }

    private static com.google.api.client.auth.oauth2.Credentials getCredentials(final NetHttpTransport httpTransport) throws IOException {
        // Load client secrets.
        java.io.File clientSecretJson = new java.io.File(CREDENTIALS_FILE_PATH);
        if (!clientSecretJson.exists()) {
            throw new IOException("Client Secret JSON file not found: " + CREDENTIALS_FILE_PATH);
        }

        // Set up authorization code flow.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, JacksonFactory.getDefaultInstance(),
                "https://oauth2.googleapis.com/token",
                "https://oauth2.googleapis.com/revoke",
                Collections.singleton(DriveScopes.DRIVE_FILE))
                .setDataStoreFactory(new FileDataStoreFactory(null))
                .setAccessType("offline")
                .build();

        // Authorize.
        return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    }
}

在代码中,替换以下内容:

  • CREDENTIALS_FILE_PATH:指向你的Google Cloud凭据文件的路径。
  • UPLOAD_FILE_PATH:要上传到Google Drive的本地文件的路径。

编译并运行示例:

代码语言:javascript
复制
javac -cp "lib/*" DriveQuickstart.java
java -cp ".:lib/*" DriveQuickstart

运行后,程序将提示你打开浏览器并授权应用程序。完成授权后,程序将上传指定的文件到Google Drive,并显示文件的ID。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券