与云端硬盘界面的“打开方式”上下文菜单集成

当用户选择文件并点击云端硬盘界面的“打开方式” 菜单项时,云端硬盘会将用户重定向 到在配置云端硬盘界面 集成中定义的该应用的打开网址。

如果您在配置云端硬盘界面集成时选中了“导入”框,用户可以选择应用专用文件和 Google Workspace 文件的组合来打开。配置云端硬盘界面集成时,应用专用文件在“默认 MIME 类型”和“默认文件扩展名”字段中定义,而 Google Workspace 文件在“辅助 MIME 类型”和“辅助文件扩展名”字段中定义。

对于用户要打开的每个文件,云端硬盘都会根据您定义的默认 MIME 类型和辅助 MIME 类型检查 MIME 类型:

  • 对于在“默认 MIME 类型”字段中定义的 MIME 类型,文件 ID 会 传递给您的应用。如需了解如何处理应用专用文件, 请参阅处理应用专用文档的打开网址

  • 对于在“辅助 MIME 类型”字段中定义的 MIME 类型,云端硬盘界面会显示一个对话框,询问用户要将 Google Workspace 文件转换为哪种文件类型。例如,如果您在云端硬盘界面中选择了一个 Google 文档文件,并且“辅助 MIME 类型”字段建议您的应用支持 text/plain 或 application/pdf,则云端硬盘界面会询问用户是否要转换为纯文本或 PDF。

    如需了解如何处理 Google Workspace 文件,请参阅处理 Google Workspace 文档的打开网址。 如需查看 Google Workspace 文档和 MIME 类型转换格式的列表, 请参阅 Google Workspace 文档的导出 MIME 类型

处理应用专用文档的打开网址

配置云端硬盘界面 集成中所述, 您的应用会收到包含模板变量,其中包含供您的应用打开 文件的信息。您的应用会在 state 参数中收到一组默认的模板变量 。应用专用打开网址的默认 state 信息如下:

{
  "ids": ["ID"],
  "resourceKeys":{"RESOURCE_KEYS":"RESOURCE_KEYS"},
  "action":"open",
  "userId":"USER_ID"
}

此输出包括以下值:

  • ID:父文件夹的 ID。
  • RESOURCE_KEYS:文件 ID 的 JSON 字典,映射到 各自的资源键。
  • open:正在执行的操作。使用打开网址时,该值为 open
  • USER_ID:唯一标识用户的个人资料 ID。

您的应用必须按照以下步骤对该请求执行操作:

  1. 验证 action 字段的值是否为 open,以及是否存在 ids 字段。
  2. 使用 userId 值为用户创建新会话。如需详细了解已登录用户,请参阅用户和新事件
  3. 使用 files.get 方法检查 权限、提取文件元数据,并使用 ID 值下载文件内容。
  4. 如果在请求中设置了 resourceKeys,请设置 X-Goog-Drive-Resource-Keys 请求标头。如需详细了解 资源键,请参阅使用资源键访问通过链接共享的文件 。

state 参数采用网址编码,因此您的应用必须处理转义字符并将其解析为 JSON。

处理 Google Workspace 文档的打开网址

配置云端硬盘界面 集成中所述,您的应用会在一组默认的 模板变量中收到一个state参数。Google Workspace 打开网址的默认 state 信息如下:

{
  "exportIds": ["ID"],
  "resourceKeys":{"RESOURCE_KEYS":"RESOURCE_KEYS"},
  "action":"open",
  "userId":"USER_ID"
}

此输出包括以下值:

  • EXPORT_ID:以逗号分隔的正在导出的文件 ID 列表。仅在打开 Google Workspace 文件时使用。
  • RESOURCE_KEYS:文件 ID 的 JSON 字典,映射到 各自的资源键。
  • open:正在执行的操作。使用打开网址时,该值为 open
  • USER_ID:标识用户的个人资料 ID。

您的应用必须按照以下步骤对该请求执行操作:

  1. 通过检测 state 字段中的 open 值以及是否存在 exportIds 字段,验证此请求是否为打开文件的请求。

  2. 使用 files.get 方法检查 权限、提取文件元数据,并使用 EXPORT_ID 值确定 MIME 类型。

  3. 使用 files.export 方法转换文件内容。以下代码示例展示了如何将 Google Workspace 文档导出为请求的 MIME 类型。

  4. 如果在请求中设置了 resourceKey,请设置 X-Goog-Drive-Resource-Keys 请求标头。如需详细了解 资源键,请参阅使用资源键访问通过链接共享的文件 。

    Java

    drive/snippets/drive_v3/src/main/java/ExportPdf.java
    import com.google.api.client.googleapis.json.GoogleJsonResponseException;
    import com.google.api.client.http.HttpRequestInitializer;
    import com.google.api.client.http.javanet.NetHttpTransport;
    import com.google.api.client.json.gson.GsonFactory;
    import com.google.api.services.drive.Drive;
    import com.google.api.services.drive.DriveScopes;
    import com.google.auth.http.HttpCredentialsAdapter;
    import com.google.auth.oauth2.GoogleCredentials;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Arrays;
    
    /* Class to demonstrate use-case of drive's export pdf. */
    public class ExportPdf {
    
      /**
       * Download a Document file in PDF format.
       *
       * @param realFileId file ID of any workspace document format file.
       * @return byte array stream if successful, {@code null} otherwise.
       * @throws IOException if service account credentials file not found.
       */
      public static ByteArrayOutputStream exportPdf(String realFileId) throws IOException {
        // Load pre-authorized user credentials from the environment.
        // TODO(developer) - See https://developers.google.com/identity for
        // guides on implementing OAuth2 for your application.
        GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
            .createScoped(Arrays.asList(DriveScopes.DRIVE_FILE));
        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
            credentials);
    
        // Build a new authorized API client service.
        Drive service = new Drive.Builder(new NetHttpTransport(),
            GsonFactory.getDefaultInstance(),
            requestInitializer)
            .setApplicationName("Drive samples")
            .build();
    
        OutputStream outputStream = new ByteArrayOutputStream();
        try {
          service.files().export(realFileId, "application/pdf")
              .executeMediaAndDownloadTo(outputStream);
    
          return (ByteArrayOutputStream) outputStream;
        } catch (GoogleJsonResponseException e) {
          // TODO(developer) - handle error appropriately
          System.err.println("Unable to export file: " + e.getDetails());
          throw e;
        }
      }
    }

    Python

    drive/snippets/drive-v3/file_snippet/export_pdf.py
    import io
    
    import google.auth
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    from googleapiclient.http import MediaIoBaseDownload
    
    
    def export_pdf(real_file_id):
      """Download a Document file in PDF format.
      Args:
          real_file_id : file ID of any workspace document format file
      Returns : IO object with location
    
      Load pre-authorized user credentials from the environment.
      TODO(developer) - See https://developers.google.com/identity
      for guides on implementing OAuth2 for the application.
      """
      creds, _ = google.auth.default()
    
      try:
        # create drive api client
        service = build("drive", "v3", credentials=creds)
    
        file_id = real_file_id
    
        # pylint: disable=maybe-no-member
        request = service.files().export_media(
            fileId=file_id, mimeType="application/pdf"
        )
        file = io.BytesIO()
        downloader = MediaIoBaseDownload(file, request)
        done = False
        while done is False:
          status, done = downloader.next_chunk()
          print(f"Download {int(status.progress() * 100)}.")
    
      except HttpError as error:
        print(f"An error occurred: {error}")
        file = None
    
      return file.getvalue()
    
    
    if __name__ == "__main__":
      export_pdf(real_file_id="1zbp8wAyuImX91Jt9mI-CAX_1TqkBLDEDcr2WeXBbKUY")

    Node.js

    drive/snippets/drive_v3/file_snippets/export_pdf.js
    import {GoogleAuth} from 'google-auth-library';
    import {google} from 'googleapis';
    
    /**
     * Exports a Google Doc as a PDF.
     * @param {string} fileId The ID of the file to export.
     * @return {Promise<number>} The status of the export request.
     */
    async function exportPdf(fileId) {
      // Authenticate with Google and get an authorized client.
      // TODO (developer): Use an appropriate auth mechanism for your app.
      const auth = new GoogleAuth({
        scopes: 'https://www.googleapis.com/auth/drive',
      });
    
      // Create a new Drive API client (v3).
      const service = google.drive({version: 'v3', auth});
    
      // Export the file as a PDF.
      const result = await service.files.export({
        fileId,
        mimeType: 'application/pdf',
      });
    
      // Print the status of the export.
      console.log(result.status);
      return result.status;
    }

    PHP

    drive/snippets/drive_v3/src/DriveExportPdf.php
    <?php
    use Google\Client;
    use Google\Service\Drive;
    function exportPdf()
    {
        try {
            $client = new Client();
            $client->useApplicationDefaultCredentials();
            $client->addScope(Drive::DRIVE);
            $driveService = new Drive($client);
            $realFileId = readline("Enter File Id: ");
            $fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
            $fileId = $realFileId;
            $response = $driveService->files->export($fileId, 'application/pdf', array(
                'alt' => 'media'));
            $content = $response->getBody()->getContents();
            return $content;
    
        }  catch(Exception $e) {
             echo "Error Message: ".$e;
        }
    
    }

将转换后的文件显示为只读文件,或显示一个对话框,让用户将文件另存为新文件类型。

state 参数采用网址编码,因此您的应用必须处理转义字符并将其解析为 JSON。

用户和新事件

云端硬盘应用应将所有“打开方式”事件视为潜在的登录事件。某些用户可能拥有多个账号,因此 state 参数中的用户 ID 可能与当前会话不匹配。如果 state 参数中的用户 ID 与当前会话不匹配,请结束应用的当前会话,并以请求的用户身份登录。

除了从 Google 云端硬盘界面打开应用之外,应用还可以 显示文件选择器,以便从应用内选择内容。如需了解 详情,请参阅 Google Picker