如何从 MS Graph 获取工作文档预览 link

How to get a working document preview link from MS Graph

我正在尝试通过 MS Graph 预览 link 用户最近的文档。不幸的是 link 端点 returns 不工作。

为了获取有关文件的信息,我首先调用 GET https://graph.microsoft.com/beta/me/drive/recent。然后我从 parentReference 属性.

复制驱动器 ID 和文档的 ID

要获得预览 link 我使用端点 POST https://graph.microsoft.com/beta/drives/<DriveID>/items/<DocumentID>/preview 这很好用。但是,当我单击 link 时,出现错误 "This item might not exist or is no longer avaiable"。该文档存在于 SharePoint 上,否则它不会出现在 "recent documents" 下。 url 看起来像这样:https://www.onedrive.com/embed?webUrl=xyz.sharepoint.com/sites/nameOfTheSite/docLibName&id=sites/nameOfTheSite/DocLibName&embed=xxx&authToken=xxx

我希望 /preview 端点的响应应该 return 有效 url。 O365 需要做一些配置吗?

非常感谢

嵌入 link (preview) 在您的示例中看起来 有效 因为 id 参数指的是 容器(库):sites/nameOfTheSite/DocLibName。它不受支持,而是嵌入 link 应该引用 文件

问题很可能是由于 itemId:

https://graph.microsoft.com/beta/drives/<DriveID>/items/<DocumentID>/preview
                                                        ^^^^^^^^^^^^   

在您的示例中,它似乎指的是库而不是文件。确保指定了正确的 itemId

例如,https://graph.microsoft.com/v1.0/me/drive/recent端点returns以下负载:

{
   "value" : {
       //another properties are omitted for a clarity
       //... 
       "remoteItem": {
           "id": "01ECKZLCWSR7F76B64KZFL7I3QGZVPJELU"
           //...   
            "parentReference": {
                "driveId": "b!79yKq-2MdkSDnQ7_1Pf3FOkRyDCajpRIvqtA7UrsEO-vu3D_qkpaT50Y6CMcSmFv",
                "driveType": "documentLibrary",
                "id": "01ECKZLCV6Y2GOVW7725BZO354PWSELRRZ"
            },
       }
   }
}

哪里

  • remoteItem.Id - 对应于文件的项目 ID
  • remoteItem.parentReference.driveId - 对应于驱动器 ID

Dont get confused with remoteItem.parentReference.Id which corresponds to item id of library