使用Graph API的openExtension为OneDrive项添加自定义属性失败,请求解决方案
解决OneDrive项添加OpenTypeExtension时的invalidRequest错误
你的请求格式不符合Graph API规范,导致invalidRequest错误,以下是修正方案:
1. 首次创建自定义扩展(POST请求)
创建OpenTypeExtension必须通过POST请求到DriveItem的extensions子资源端点,而非直接PATCH DriveItem本身,正确请求如下:
POST https://graph.microsoft.com/v1.0/drives/b!iy7xxxxxxu5XOy-gixxxxd2HB_kk/items/016xxO3N6xxxxxxxxx3IHZxx7Y6/extensions Content-Type: application/json
请求体(无需嵌套openTypeExtension字段):
{ "@odata.type": "microsoft.graph.openTypeExtension", "extensionName": "com.contoso.customAttributes", "_AlreadyCheck": "YES" }
2. 更新已存在的自定义扩展(PATCH请求)
如果扩展已创建,需要更新属性时,直接PATCH对应的扩展资源,端点需指定扩展名称:
PATCH https://graph.microsoft.com/v1.0/drives/b!iy7xxxxxxu5XOy-gixxxxd2HB_kk/items/016xxO3N6xxxxxxxxx3IHZxx7Y6/extensions/com.contoso.customAttributes Content-Type: application/json
请求体:
{ "_AlreadyCheck": "YES" }
关键注意事项
- 扩展名称
extensionName必须遵循反向域名格式(如com.contoso.xxx),确保唯一性 - 验证Drive ID和DriveItem ID的正确性,避免拼写错误
- 确保应用拥有足够权限:需
Files.ReadWrite(委派权限)或Files.ReadWrite.All(应用权限)
内容的提问来源于stack exchange,提问作者John Park
相关产品推荐
相关产品推荐

