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

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

如果您在配置云端硬盘界面集成时勾选了“导入”复选框,则用户可以选择要打开的应用专用文件和 Google Workspace 文件的组合。配置云端硬盘界面集成时,应用专用文件会在“默认 MIME 类型”和“默认文件扩展名”字段中定义,而 Google Workspace 文件则会在“辅助 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 参数中接收一组默认的模板变量。应用专属的 Open 网址 的默认 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 Open 网址的默认 state 信息为:

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

此输出包括以下值:

  • EXPORT_ID:要导出的文件 ID 的逗号分隔列表(仅在打开内置 Google 文档时使用)。
  • 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
    /**
     * Download a Document file in PDF format
     * @param{string} fileId file ID
     * @return{obj} file status
     * */
    async function exportPdf(fileId) {
      const {GoogleAuth} = require('google-auth-library');
      const {google} = require('googleapis');
    
      // Get credentials and build service
      // TODO (developer) - Use appropriate auth mechanism for your app
      const auth = new GoogleAuth({
        scopes: 'https://www.googleapis.com/auth/drive',
      });
      const service = google.drive({version: 'v3', auth});
    
      try {
        const result = await service.files.export({
          fileId: fileId,
          mimeType: 'application/pdf',
        });
        console.log(result.status);
        return result;
      } catch (err) {
        // TODO(developer) - Handle error
        throw err;
      }
    }

    PHP

    drive/snippets/drive_v3/src/DriveExportPdf.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 选择器