命名空间:microsoft.graph
允许使用 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
名称 |
说明 |
Authorization |
持有者 {token}。 必填。 详细了解 身份验证和授权。 |
Content-Type |
application/json. 必需。 |
示例
请求
GET /me/drive/root/subscriptions/socketIo
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].Subscriptions["{subscription-id}"].GetAsync();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
mgc drives items subscriptions get --drive-id {drive-id} --drive-item-id {driveItem-id} --subscription-id {subscription-id}
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
//other-imports
)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
subscriptions, err := graphClient.Drives().ByDriveId("drive-id").Items().ByDriveItemId("driveItem-id").Subscriptions().BySubscriptionId("subscription-id").Get(context.Background(), nil)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
Subscription result = graphClient.drives().byDriveId("{drive-id}").items().byDriveItemId("{driveItem-id}").subscriptions().bySubscriptionId("{subscription-id}").get();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
let subscription = await client.api('/me/drive/root/subscriptions/socketIo')
.get();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$result = $graphServiceClient->drives()->byDriveId('drive-id')->items()->byDriveItemId('driveItem-id')->subscriptions()->bySubscriptionId('subscription-id')->get()->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Files
Get-MgDriveItemSubscription -DriveId $driveId -DriveItemId $driveItemId -SubscriptionId $subscriptionId
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
result = await graph_client.drives.by_drive_id('drive-id').items.by_drive_item_id('driveItem-id').subscriptions.by_subscription_id('subscription-id').get()
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
如果成功,此方法在 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。
以下示例演示如何在 JavaScript 中将 与 socket.io 配合使用 notificationUrl
。
<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>