如何在根目录中创建文件夹并在Android的Google Drive REST API v3中获取其ID

https://developers.google.com/drive/v3/web/folder上的“使用文件夹”指南中,这是创建文件夹并获取其ID的代码。

File fileMetadata = new File();
fileMetadata.setName("Invoices");
fileMetadata.setMimeType("application/vnd.google-apps.folder");

File file = driveService.files().create(fileMetadata)
        .setFields("id")
        .execute();
System.out.println("Folder ID: " + file.getId());

但我只是不知道上面的代码片段中的driveService是什么?谁能指导我。

Tanaike

driveService是在Drive service = getDriveService();JAVA的Quickstart中定义您可以通过将快速入门的从更改为service使用脚本driveService

如果要使用快速入门,则可以使用以下示例脚本。我已将您的脚本添加到快速入门中。如果使用此脚本,请通过以下设置

脚本修改后的快速入门:

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.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;

import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.*;
import com.google.api.services.drive.Drive;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class Quickstart {
    /** Application name. */
    private static final String APPLICATION_NAME =
        "Drive API Java Quickstart";

    /** Directory to store user credentials for this application. */
    private static final java.io.File DATA_STORE_DIR = new java.io.File(
        System.getProperty("user.home"), ".credentials/drive-java-quickstart");

    /** Global instance of the {@link FileDataStoreFactory}. */
    private static FileDataStoreFactory DATA_STORE_FACTORY;

    /** 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();
            DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
        } 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 {
        // Load client secrets.
        InputStream in =
            Quickstart.class.getResourceAsStream("/client_secret.json");
        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(DATA_STORE_FACTORY)
                .setAccessType("offline")
                .build();
        Credential credential = new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize("user");
        System.out.println(
                "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
        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 void main(String[] args) throws IOException {
        // Build a new authorized API client service.
        Drive driveService = getDriveService();

        // Your script is here.
        File fileMetadata = new File();
        fileMetadata.setName("Invoices");
        fileMetadata.setMimeType("application/vnd.google-apps.folder");

        File file = driveService.files().create(fileMetadata)
                .setFields("id")
                .execute();
        System.out.println("Folder ID: " + file.getId());
    }
}

结果:

Folder ID: ### folder ID ###

如果我误解了您的问题,对不起。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Java中的Google Drive API v3获取上载文件到Google Drive的可共享链接

如何在Google Drive API v3 PHP中的特定文件夹中列出文件

如何在Java中通过Google Drive API v3获取文件的直接下载链接

如何在Google Drive API v3中获取文件的“ lastModified”?

Google Drive API v3在Java中获取根文件夹ID

如何使用Google API在Team Drive中创建文件夹?

如何在Team Drive中创建文件夹?

在根目录下创建文件夹并将文件上传到该文件夹google drive api

如何在Google Drive REST API中查看文件的权限?

使用api rest在Google驱动器中的创建文件夹期间未定义file.id

Google Drive Rest API

在C#中的Google Drive Api v3中获取根ID

如何在Flutter应用中使用Google Drive API创建文件夹?

获取/生成Google Drive API v3中上传文件的共享链接

如何从Google Drive REST API v3获取共享链接?

如何在Google Drive API中按路径查找文件夹ID?

Google Drive API Python客户端:批量创建时如何检索已创建文件夹ID的列表?

列出目录中ID不是Google Drive API v3和C#中特定值的文件夹

如何使用Google的Python Drive Client API在Team Drive中创建文件夹?

如何在Google Drive Android API中获取已创建文件夹的DriveId

如何获取Google Drive Rest API调用的访问令牌?

Google Drive REST API的问题

如何从 google drive api v3 获取文件列表?

使用 Google Drive REST API 创建文件访问请求

403:在google drive api php中创建文件夹时权限不足

Drive API - 在父文件夹中创建文件夹不起作用

如何在 google drive api 中复制文件夹?

如何从 Google Drive 上传和下载文件(使用 Rest Api v3)

如何使用 Google Drive API v3 将可恢复上传到 Python 中的共享文件夹?