访问共享 DriveItem
本文内容
命名空间:microsoft.graph
通过使用 shareId 或共享 URL 访问共享 DriveItem 或共享项目集合。
要与此 API 一起使用共享 URL,应用需要将此 URL 转换为共享令牌 。
此 API 可用于以下国家级云部署 。
全局服务
美国政府 L4
美国政府 L5 (DOD)
由世纪互联运营的中国
✅
✅
✅
✅
权限
为此 API 选择标记为最低特权的权限。 只有在应用需要它时 ,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型 。 要了解有关这些权限的详细信息,请参阅 权限参考 。
权限类型
最低特权权限
更高特权权限
委派(工作或学校帐户)
Files.ReadWrite
Files.ReadWrite.All、Sites.ReadWrite.All
委派(个人 Microsoft 帐户)
Files.ReadWrite
Files.ReadWrite.All
应用程序
Files.ReadWrite.All
Sites.ReadWrite.All
HTTP 请求
GET /shares/{shareIdOrEncodedSharingUrl}
路径参数
参数名称
值
说明
shareIdOrEncodedSharingUrl
string
必填。 API 返回的共享令牌或正确编码的共享 URL。
编码共享 URL
若要编码共享 URL,请使用以下逻辑:
首先,使用 base64 编码 URL。
删除值末尾的 =
字符,将 /
替换成 _
,将 +
替换成 -
,从而将 base64 编码结果转换成未填充的 base64url 格式 。
将 u!
追加到字符串的开头。
例如,若要对 URL 进行 C# 编码,请使用以下代码:
string sharingUrl = "https://onedrive.live.com/redir?resid=1231244193912!12&authKey=1201919!12921!1";
string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharingUrl));
string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/','_').Replace('+','-');
名称
类型
说明
Prefer
string
可选。 设置为下面所述的值之 prefer
一。
名称
说明
redeemSharingLink
如果 shareIdOrEncodedSharingUrl 是共享链接,请授予调用方对该项的持久访问权限
redeemSharingLinkIfNecessary
与 redeemSharingLink 相同,但仅保证在此请求期间授予访问权限
redeemSharingLink 应被视为等效于调用方导航到浏览器 (接受共享手势) 的共享链接,而 redeemSharingLinkIfNecessary 的意图只是查看链接的元数据。
响应
如果成功,此方法在响应正文中返回 200 OK
响应代码和 sharedDriveItem 资源。
示例
请求
以下示例显示了检索共享项的请求:
GET /shares/{shareIdOrEncodedSharingUrl}
// 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.Shares["{sharedDriveItem-id}"].GetAsync();
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc-beta shares get --shared-drive-item-id {sharedDriveItem-id}
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
//other-imports
)
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
shares, err := graphClient.Shares().BySharedDriveItemId("sharedDriveItem-id").Get(context.Background(), nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
SharedDriveItem result = graphClient.shares().bySharedDriveItemId("{sharedDriveItem-id}").get();
const options = {
authProvider,
};
const client = Client.init(options);
let sharedDriveItem = await client.api('/shares/{shareIdOrEncodedSharingUrl}')
.version('beta')
.get();
<?php
use Microsoft\Graph\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$result = $graphServiceClient->shares()->bySharedDriveItemId('sharedDriveItem-id')->get()->wait();
Import-Module Microsoft.Graph.Beta.Files
Get-MgBetaShareSharedDriveItemSharedDriveItem -SharedDriveItemId $sharedDriveItemId
from msgraph import GraphServiceClient
graph_client = GraphServiceClient(credentials, scopes)
result = await graph_client.shares.by_shared_drive_item_id('sharedDriveItem-id').get()
响应
以下示例显示了相应的响应。
HTTP/1.1 200 OK
Content-type: application/json
{
"id": "B64397C8-07AE-43E4-920E-32BFB4331A5B",
"name": "contoso project.docx",
"owner": {
"user": {
"id": "98E88F1C-F8DC-47CC-A406-C090248B30E5",
"displayName": "Ryan Gregg"
}
}
}
直接访问共享项目
虽然 SharedDriveItem 包含一些有用的信息,但大多数应用都希望直接访问共享 DriveItem 。
SharedDriveItem 资源包括根 关系和项目 关系,这些关系可以访问共享项范围内的内容。
示例(单个文件)
请求
通过请求 driveItem 关系,将返回共享的 DriveItem 。
GET /shares/{shareIdOrUrl}/driveItem
// 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.Shares["{sharedDriveItem-id}"].DriveItem.GetAsync();
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc-beta shares drive-item get --shared-drive-item-id {sharedDriveItem-id}
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
//other-imports
)
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
driveItem, err := graphClient.Shares().BySharedDriveItemId("sharedDriveItem-id").DriveItem().Get(context.Background(), nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
DriveItem result = graphClient.shares().bySharedDriveItemId("{sharedDriveItem-id}").driveItem().get();
const options = {
authProvider,
};
const client = Client.init(options);
let driveItem = await client.api('/shares/{shareIdOrUrl}/driveItem')
.version('beta')
.get();
<?php
use Microsoft\Graph\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$result = $graphServiceClient->shares()->bySharedDriveItemId('sharedDriveItem-id')->driveItem()->get()->wait();
Import-Module Microsoft.Graph.Beta.Files
Get-MgBetaShareDriveItem -SharedDriveItemId $sharedDriveItemId
from msgraph import GraphServiceClient
graph_client = GraphServiceClient(credentials, scopes)
result = await graph_client.shares.by_shared_drive_item_id('sharedDriveItem-id').drive_item.get()
响应
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": "9FFFDB3C-5B87-4062-9606-1B008CA88E44",
"name": "contoso project.docx",
"eTag": "2246BD2D-7811-4660-BD0F-1CF36133677B,1",
"file": {},
"size": 109112
}
示例(共享文件夹)
请求
通过请求 driveItem 关系并展开子 集合,将同时返回共享的 DriveItem 以及共享文件夹内的文件。
GET /shares/{shareIdOrUrl}/driveItem?$expand=children
// 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.Shares["{sharedDriveItem-id}"].DriveItem.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Expand = new string []{ "children" };
});
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc-beta shares drive-item get --shared-drive-item-id {sharedDriveItem-id} --expand "children"
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphshares "github.com/microsoftgraph/msgraph-beta-sdk-go/shares"
//other-imports
)
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
requestParameters := &graphshares.ShareItemDriveItemRequestBuilderGetQueryParameters{
Expand: [] string {"children"},
}
configuration := &graphshares.ShareItemDriveItemRequestBuilderGetRequestConfiguration{
QueryParameters: requestParameters,
}
driveItem, err := graphClient.Shares().BySharedDriveItemId("sharedDriveItem-id").DriveItem().Get(context.Background(), configuration)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
DriveItem result = graphClient.shares().bySharedDriveItemId("{sharedDriveItem-id}").driveItem().get(requestConfiguration -> {
requestConfiguration.queryParameters.expand = new String []{"children"};
});
const options = {
authProvider,
};
const client = Client.init(options);
let driveItem = await client.api('/shares/{shareIdOrUrl}/driveItem')
.version('beta')
.expand('children')
.get();
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Shares\Item\DriveItem\DriveItemRequestBuilderGetRequestConfiguration;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestConfiguration = new DriveItemRequestBuilderGetRequestConfiguration();
$queryParameters = DriveItemRequestBuilderGetRequestConfiguration::createQueryParameters();
$queryParameters->expand = ["children"];
$requestConfiguration->queryParameters = $queryParameters;
$result = $graphServiceClient->shares()->bySharedDriveItemId('sharedDriveItem-id')->driveItem()->get($requestConfiguration)->wait();
Import-Module Microsoft.Graph.Beta.Files
Get-MgBetaShareDriveItem -SharedDriveItemId $sharedDriveItemId -ExpandProperty "children"
from msgraph import GraphServiceClient
from msgraph.generated.shares.item.drive_item.drive_item_request_builder import DriveItemRequestBuilder
graph_client = GraphServiceClient(credentials, scopes)
query_params = DriveItemRequestBuilder.DriveItemRequestBuilderGetQueryParameters(
expand = ["children"],
)
request_configuration = DriveItemRequestBuilder.DriveItemRequestBuilderGetRequestConfiguration(
query_parameters = query_params,
)
result = await graph_client.shares.by_shared_drive_item_id('sharedDriveItem-id').drive_item.get(request_configuration = request_configuration)
响应
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": "9FFFDB3C-5B87-4062-9606-1B008CA88E44",
"name": "Contoso Project",
"eTag": "2246BD2D-7811-4660-BD0F-1CF36133677B,1",
"folder": {},
"size": 10911212,
"children": [
{
"id": "AFBBDD79-868E-452D-AD4D-24697D4A4044",
"name": "Propsoal.docx",
"file": {},
"size": 19001
},
{
"id": "A91FE90A-2F2C-4EE6-B412-C4FFBA3F71A6",
"name": "Update to Proposal.docx",
"file": {},
"size": 91001
}
]
}
错误响应
有关错误返回方式的详细信息,请参阅错误 响应 一文。
对于 OneDrive for Business 和 SharePoint,共享 API 始终需要身份验证,并且不能用于访问没有用户上下文的匿名共享内容。