用户选择文件并点击云端硬盘界面的“打开方式”菜单项后,云端硬盘会将用户重定向到配置云端硬盘界面集成中定义的该应用的 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。
您的应用必须按照以下步骤处理此请求:
- 验证
action
字段的值是否为open
,以及ids
字段是否存在。 - 使用
userId
值为用户创建一个新会话。如需详细了解已登录用户,请参阅用户和新事件。 - 使用
files.get
方法检查权限、提取文件元数据,以及使用ID
值下载文件内容。 - 如果请求中设置了
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。
您的应用必须按照以下步骤处理此请求:
通过检测
state
字段中的open
值和是否存在exportIds
字段,验证这是打开文件的请求。使用
files.get
方法检查权限、提取文件元数据,以及使用EXPORT_ID
值确定 MIME 类型。使用
files.export
方法转换文件内容。以下代码示例展示了如何将 Google Workspace 文档导出到请求的 MIME 类型。如果请求中设置了
resourceKey
,请设置X-Goog-Drive-Resource-Keys
请求标头。如需详细了解资源密钥,请参阅使用资源密钥访问链接共享文件。Java
Python
Node.js
PHP
将转换后的文件显示为只读,或显示一个对话框,供用户将文件另存为新文件类型。
state
参数采用网址编码形式,因此您的应用必须处理转义字符并将其解析为 JSON。
用户和新活动
云端硬盘应用应将所有“打开方式”事件视为潜在登录。某些用户可能有多个帐号,因此 state
参数中的用户 ID 可能与当前会话不匹配。如果 state
参数中的用户 ID 与当前会话不匹配,请结束应用的当前会话,并以请求的用户的身份登录。
相关主题
除了从 Google 云端硬盘界面打开应用之外,应用还可以显示文件选择器,用于从应用内选择内容。如需了解详情,请参阅 Google 选择器。