You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Microsoft Graph获取OneDrive文件共享链接的密码保护及过期信息

获取OneDrive共享链接的密码保护与过期日期

我来帮你解决这个问题!你之前调用graphClient.users('email').drive().items(id).permissions()没拿到想要的信息,是因为默认返回的权限集合不包含共享链接的详细属性——这些关键信息(密码保护状态、过期日期)都存在权限条目的link字段里,你需要明确指定获取这个字段才行。

使用Microsoft Graph SDK的方式

你只需要在调用时添加.select('id,link')来指定要返回的属性,就能拿到共享链接的完整信息。以JavaScript SDK为例:

// 初始化graphClient后执行以下代码
const itemPermissions = await graphClient
  .users('target-user@example.com')
  .drive()
  .items('your-item-id')
  .permissions()
  .select('id,link') // 明确请求link属性
  .get();

// 筛选出包含共享链接的权限条目(不是所有权限都是共享链接)
const sharedLinks = itemPermissions.value.filter(perm => perm.link);

// 遍历提取所需信息
sharedLinks.forEach(linkPerm => {
  console.log(`共享链接ID: ${linkPerm.id}`);
  console.log(`是否受密码保护: ${linkPerm.link.passwordProtected}`);
  console.log(`过期日期: ${linkPerm.link.expirationDateTime || '无过期设置'}`);
  console.log(`链接地址: ${linkPerm.link.webUrl}`);
});

使用直接REST API调用的方式

如果不用SDK,你可以发送GET请求到以下端点,通过$select参数指定要获取link属性:

GET https://graph.microsoft.com/v1.0/users/target-user@example.com/drive/items/your-item-id/permissions?$select=id,link
Authorization: Bearer {你的访问令牌}

返回的响应中,每个包含link字段的条目就是一个共享链接,示例响应结构如下:

{
  "value": [
    {
      "id": "abc123",
      "link": {
        "type": "edit",
        "scope": "organization",
        "passwordProtected": false,
        "expirationDateTime": "2024-12-31T23:59:59Z",
        "webUrl": "https://contoso-my.sharepoint.com/personal/user_contoso_com/_layouts/15/guestaccess.aspx?..."
      }
    },
    // 其他非共享链接的权限条目(无link字段)
  ]
}

注意事项

  • 不是所有权限条目都是共享链接:比如直接给特定用户/组赋予的权限不会包含link字段,所以需要筛选出存在link的条目。
  • 权限要求:你的访问令牌需要具备Files.Read、Files.ReadWrite或更高的权限(具体取决于你要访问的内容范围)。

内容的提问来源于stack exchange,提问作者yael alfasi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 13:42:50