从 Drive Activity API v1 迁移
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本指南介绍了 Google Drive Activity API v1 和 v2 之间的区别,以及如何更改 v1 应用以支持 v2 API。
授权
v1 API 使用以下范围:
https://www.googleapis.com/auth/activity
v2 API 需要以下任一范围:
https://www.googleapis.com/auth/drive.activity
https://www.googleapis.com/auth/drive.activity.readonly
资源名称
在 v1 API 中,Google 云端硬盘内容和用户等对象的标识符是不可透的字符串。在 v2 API 中,通常使用资源名称引用这些对象。如需了解详情,请参阅 Cloud API 设计指南。
这些标识符通常可以转换。例如,v2 API 中的云端硬盘内容使用资源名称 items/ITEM_ID_V1
进行引用。
请求
v2 的请求格式与 v1 的请求格式类似。具体而言,您仍然可以请求云端硬盘文件或云端硬盘父级的活动记录,但请注意,您必须将这些请求参数的格式设置为资源名称,方法是将 items/
添加为这些参数的前缀。
“分组”现在称为合并,并且 source
和 userId
请求参数已移除。
此外,我们还提供了新的过滤条件选项,可让您限制响应中返回的活动记录数据类型。
操作
在 v1 API 中,活动类型和与该活动关联的数据位于单独的字段中。例如,如果 primaryEventType
字段包含值 move
,则应用会假定顶级 move
字段已填充添加和移除的父级。
在 v2 API 中,这些字段不再是不同的字段。ActionDetail
消息只能设置一个字段。它表示操作类型,并包含与该操作相关的详细信息。例如,表示移动的 ActionDetail
仅会设置 move
字段,该字段会列出添加和移除的父项。
v1 API primaryEventType
字段大致与 v2 primaryActionDetail
对应。
行为者
在 v1 API 中,如果操作者是已知用户,则返回的 activity 包含 User
;对于特殊情况,还可以包含顶级字段(例如 fromUserDeletion
)。
在 v2 API 中,提供了一组更丰富的 Actor
类型,并且当执行者是已知用户时,系统会填充 user.knownUser
。如果您的应用需要有关用户的详细信息,可以通过将 KnownUser
字段 personName
传递给 people.get
方法,从 People API 中查询这些信息。
目标
在 v1 API 中,目标始终是云端硬盘项。在 v2 API 中,目标可以是云端硬盘中的其他类型的对象。例如,对云端硬盘所做的更改的目标类型为 Drive
。系统仍会返回共享云端硬盘的根文件夹(在 root
字段中显示为 DriveItem
),但它不是 activity 的直接目标。类似的概念也适用于 FileComment
资源,其 parent
字段引用包含目标评论会话的云端硬盘项。
合并的活动
在 v1 API 中,设置汇总(“分组”)策略后,响应样式会发生变化。具体而言,启用合并后,每个 activity 都包含组成 singleEvents
和一个 combinedEvent
,用于汇总这些组成事件之间的共同 activity。停用合并后,combinedEvent
字段会包含每个活动的原始未合并事件。其中任何事件都可能代表多项操作,例如创建和分享。
在 v2 API 中,响应样式不会因汇总策略而改变,因为返回的 DriveActivity
始终包含一整套操作者、目标和操作。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Migrate from Drive Activity API v1\n\nThis guide explains the differences between Google Drive Activity API v1 and v2, and how\nto change your v1 application to support the v2 API.\n\nAuthorization\n-------------\n\nThe v1 API used this scope:\n\n- `https://www.googleapis.com/auth/activity`\n\nThe v2 API requires one of the following scopes:\n\n- `https://www.googleapis.com/auth/drive.activity`\n- `https://www.googleapis.com/auth/drive.activity.readonly`\n\nResource names\n--------------\n\nIn the v1 API, identifiers for objects like Google Drive items and users were\nopaque strings. In the v2 API, these objects are typically referenced using\nresource names. For more information, see the [Cloud API Design\nGuide](https://cloud.google.com/apis/design/resource_names)\n.\n\nThese identifiers can generally be converted. For example, Drive\nitems in the v2 API are referenced using the resource name\n`items/`\u003cvar translate=\"no\"\u003eITEM_ID_V1\u003c/var\u003e.\n\nRequests\n--------\n\nThe request format for v2 is similar to that of v1. Specifically, you can still\nrequest activity for a Drive file or a Drive\nancestor, though note that you must format those request parameters as [resource\nnames](#resource_names) by prefixing them with `items/`.\n\n\"Grouping\" is now called\n[Consolidation](/workspace/drive/activity/v2/requests#consolidation), and the `source` and\n`userId` request parameters have been removed.\n\nThere are also new [Filter](/workspace/drive/activity/v2/requests#filters) options that\nallow you to constrain the types of activity data returned in the response.\n\nActions\n-------\n\nIn the v1 API, the activity type, and the data associated with that activity,\nwere in separate fields. For example, if the `primaryEventType` field contained\nthe value `move`, then apps would assume that a top-level `move` field is\npopulated with the added and removed parents.\n\nIn the v2 API, these fields are no longer distinct. The\n[`ActionDetail`](/workspace/drive/activity/v2/reference/rest/v2/activity/actiondetail)\nmessage has exactly one field set. It signifies the action type and contains the\ndetails associated with that action. For example, an `ActionDetail` representing\na move only sets the `move` field, and that field lists the added and removed\nparents.\n\nThe v1 API `primaryEventType` field roughly corresponds with the v2\n`primaryActionDetail`.\n\nActors\n------\n\nIn the v1 API, the returned activity contained a `User` if the actor was a known\nuser, and optionally contained a top-level field such as `fromUserDeletion` for\nspecial cases.\n\nIn the v2 API, a richer set of\n[`Actor`](/workspace/drive/activity/v2/reference/rest/v2/activity/actor) types is\navailable, and `user.knownUser` is populated when the actor is a known user. If\nyour application needs detailed information about users, it can query it from\nthe [People API](/people) by passing the `KnownUser` field\n[`personName`](/workspace/drive/activity/v2/reference/rest/v2/activity/user#knownuser) to\nthe [`people.get`](/people/api/rest/v1/people/get) method.\n\nTargets\n-------\n\nIn the v1 API, targets were always Drive items. In the v2 API,\ntargets can be other types of objects in Drive. For example,\nchanges to a drive have a target type of\n[`Drive`](/workspace/drive/activity/v2/reference/rest/v2/activity/drive). The root folder\nof a shared drive is still returned (as a\n[`DriveItem`](/workspace/drive/activity/v2/reference/rest/v2/activity/driveitem) in the\n`root` field), but it's not the immediate target of the activity. A similar\nconcept applies to a\n[`FileComment`](/workspace/drive/activity/v2/reference/rest/v2/activity/filecomment)\nresource, whose `parent` field refers to the Drive item\ncontaining the target comment thread.\n\nConsolidated activity\n---------------------\n\nIn the v1 API, the style of response changed when a consolidation (\"grouping\")\nstrategy was set. Specifically, when consolidation was turned on, each activity\ncontained the constituent `singleEvents` and a `combinedEvent` that summarized\nthe common activity among those constituent events. When consolidation was\nturned off, the `combinedEvent` field contained the original unconsolidated\nevent for each activity. Any of these events could represent more than one\naction, such as a create along with a share.\n\nIn the v2 API, the style of response doesn't change based on the consolidation\nstrategy, as the returned\n[`DriveActivity`](/workspace/drive/activity/v2/reference/rest/v2/activity/driveactivity)\nalways contains the full set of actors, targets, and actions."]]