获取 websocket 终结点

命名空间:microsoft.graph

重要

Microsoft Graph /beta 版本下的 API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。

不支持在生产应用程序中使用这些 API。

允许您使用 socket.io 接收驱动器和列表的近实时更改通知。 Socket.io 是一种常用的 JavaScript 通知库,它利用 WebSocket。 若要了解详细信息,请参阅 socket.io。

权限

为此 API 选择标记为最低特权的权限。 只有在应用需要它时,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型。 要了解有关这些权限的详细信息,请参阅 权限参考。

权限类型 最低特权权限 更高特权权限
委派(工作或学校帐户) Files.Read Files.ReadWrite、Files.ReadWrite.All、Sites.ReadWrite.All
委派(个人 Microsoft 帐户) Files.Read Files.ReadWrite、Files.ReadWrite.All
应用程序 不支持。 不支持。

HTTP 请求

GET /me/drive/root/subscriptions/socketIo
GET /drives/{driveId}/root/subscriptions/socketIo
GET /drives/{driveId}/list/subscriptions/socketIo
GET /groups/{groupId}/drive/root/subscriptions/socketIo
GET /sites/{siteId}/lists/{listId}/drive/root/subscriptions/socketIo

示例

请求

GET /me/drive/root/subscriptions/socketIo

响应

如果成功,此方法将在响应正文中返回响应 200 OK 代码和 订阅 对象。

HTTP/1.1 200 OK
Content-type: application/json

{
  "id": "opaqueId-fj3hd7yf283jfk193726nvc2w3i2diemdu8",
  "notificationUrl": "https://f3hb0mpua.svc.ms/zbaehwg/callback?snthgk=1ff3-2345672zz831837523"
}

返回的 notificationUrl 是一个 socket.io 终结点 URL。 若要将其用于 socket.io 客户端,请拆分令牌上的 /callback? 字符串。 前面 /callback? 字符串的部分是 socket.io 终结点 URL,后面的字符串部分是必须提供给库的不透明查询字符串。

以下示例演示如何在 notificationUrl JavaScript 中使用 with socket.io。

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.8.1/socket.io.js"></script>
<script>
  // This is the notificationUrl returned from this API
  var notificationUrl = "https://f3hb0mpua.svc.ms/zbaehwg/callback?snthgk=1ff3-2345672zz831837523";
  
  // 'io' comes from the socket.io client library
  var socket = io(notificationUrl, {
    transports: ['websocket'] // Make sure to use "websocket" instead of the default value of "polling" which isn't supported
  });
  
  socket.on("connect", () => {
    console.log(`connect`, socket.id);
  });
  
  socket.on("disconnect", () => {
    // Returns "undefined" on disconnect
    console.log(`disconnect`, socket.id);
  });
  
  socket.on("notification", (data) => {
    console.log(`Notification received:`, data);
  });
</script>