DriveItem の共有リンクを作成する
[アーティクル]
09/13/2021
2 人の共同作成者
この記事の内容
名前空間: microsoft.graph
createLink アクションを使用して、共有リンクを使って DriveItem を共有できます。
createLink アクションは、呼び出し元のアプリケーションに指定されたリンクの種類が存在しない場合に、新しい共有リンクを作成します。指定された種類の共有リンクがアプリで既に存在している場合、既存の共有リンクが返されます。
DriveItem リソースは、そのリソースの先祖から共有アクセス許可を継承します。
アクセス許可
この API を呼び出すには、次のいずれかのアクセス許可が必要です。アクセス許可の選択方法などの詳細については、「アクセス許可 」を参照してください。
アクセス許可の種類
アクセス許可 (特権の小さいものから大きいものへ)
委任 (職場または学校のアカウント)
Files.ReadWrite、Files.ReadWrite.All、Sites.ReadWrite.All
委任 (個人用 Microsoft アカウント)
Files.ReadWrite、Files.ReadWrite.All
アプリケーション
Files.ReadWrite.All、Sites.ReadWrite.All
HTTP 要求
POST /drives/{driveId}/items/{itemId}/createLink
POST /groups/{groupId}/drive/items/{itemId}/createLink
POST /me/drive/items/{itemId}/createLink
POST /sites/{siteId}/drive/items/{itemId}/createLink
POST /users/{userId}/drive/items/{itemId}/createLink
要求本文
要求本文はアプリケーションが要求する共有リンクのプロパティを定義します。
要求は、次のプロパティを含む JSON オブジェクトである必要があります。
名前
型
説明
type
string
作成する共有リンクの種類。view、edit、または embed です。
password
string
作成者が設定した共有リンクのパスワード。 オプションとOneDrive個人用のみです。
expirationDateTime
string
DateTime の yyyyy-MM-ddTHH:mm:ssZ の形式の文字列は、アクセス許可の有効期限を示します。
scope
string
省略可能。 作成するリンクのスコープ。 anonymous または organization のどちらかです。
リンクの種類
type パラメーターには次の値を使用できます。
種類の値
説明
view
その DriveItem への読み取り専用リンクを作成します。
edit
その DriveItem への読み取り/書き込みリンクを作成します。
embed
その DriveItem への埋め込み可能なリンクを作成します。 このオプションは OneDrive 個人用のファイルでのみ選択可能です。
スコープの種類
scope パラメーターには次の値を使用できます。
scope パラメーターが指定されていない場合、組織の既定のリンクの種類が作成されます。
値
説明
anonymous
リンクを知っているすべてのユーザーは、サインインするこなくアクセスできます。 これには、組織外のユーザーも含まれる場合があります。 匿名リンクのサポートは、管理者により無効にされることがあります。
organization
組織 (テナント) にサインインしているユーザーは、リンクを使用してアクセスできます。 OneDrive for Business および SharePoint の場合のみ使用できます。
応答
正常に実行されると、このメソッドは要求された共有アクセス許可を表す応答本文で、単一のアクセス許可 リソースを返します。
応答は、アイテムに新しい共有リンクが作成される場合は 201 Created、既存のリンクが返される場合は 200 OK となります。
例
次の例では、ユーザーの OneDrive の {itemId} で指定された DriveItem に対して共有リンクを作成することを要求します。
共有リンクは、読み取り専用でそのリンクによってだれでも使用可能になるよう構成されます。
要求
POST /me/drive/items/{item-id}/createLink
Content-type: application/json
{
"type": "view",
"password": "ThisIsMyPrivatePassword",
"scope": "anonymous"
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var type = "view";
var password = "ThisIsMyPrivatePassword";
var scope = "anonymous";
await graphClient.Me.Drive.Items["{driveItem-id}"]
.CreateLink(type,scope,null,password,null,null)
.Request()
.PostAsync();
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
const options = {
authProvider,
};
const client = Client.init(options);
const permission = {
type: 'view',
password: 'ThisIsMyPrivatePassword',
scope: 'anonymous'
};
await client.api('/me/drive/items/{item-id}/createLink')
.post(permission);
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/v1.0/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/drive/items/{item-id}/createLink"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSMutableDictionary *payloadDictionary = [[NSMutableDictionary alloc] init];
NSString *type = @"view";
payloadDictionary[@"type"] = type;
NSString *password = @"ThisIsMyPrivatePassword";
payloadDictionary[@"password"] = password;
NSString *scope = @"anonymous";
payloadDictionary[@"scope"] = scope;
NSData *data = [NSJSONSerialization dataWithJSONObject:payloadDictionary options:kNilOptions error:&error];
[urlRequest setHTTPBody:data];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
String type = "view";
String password = "ThisIsMyPrivatePassword";
String scope = "anonymous";
graphClient.me().drive().items("{item-id}")
.createLink(DriveItemCreateLinkParameterSet
.newBuilder()
.withType(type)
.withScope(scope)
.withExpirationDateTime(null)
.withPassword(password)
.withMessage(null)
.withRetainInheritedPermissions(null)
.build())
.buildRequest()
.post();
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
応答
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": "123ABC",
"roles": ["write"],
"link": {
"type": "view",
"scope": "anonymous",
"webUrl": "https://1drv.ms/A6913278E564460AA616C71B28AD6EB6",
"application": {
"id": "1234",
"displayName": "Sample Application"
},
},
"hasPassword": true
}
会社の共有可能リンクを作成する
OneDrive for Business および SharePoint は、会社の共有可能リンクをサポートしています。
これらは匿名リンクに似ていますが、所有組織のメンバーだけが使用できる点が異なります。
会社の共有可能リンクを作成するには、scope パラメーターで値 organization を指定して使用します。
要求
POST /me/drive/items/{item-id}/createLink
Content-Type: application/json
{
"type": "edit",
"scope": "organization"
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var type = "edit";
var scope = "organization";
await graphClient.Me.Drive.Items["{driveItem-id}"]
.CreateLink(type,scope,null,null,null,null)
.Request()
.PostAsync();
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
const options = {
authProvider,
};
const client = Client.init(options);
const permission = {
type: 'edit',
scope: 'organization'
};
await client.api('/me/drive/items/{item-id}/createLink')
.post(permission);
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/v1.0/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/drive/items/{item-id}/createLink"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSMutableDictionary *payloadDictionary = [[NSMutableDictionary alloc] init];
NSString *type = @"edit";
payloadDictionary[@"type"] = type;
NSString *scope = @"organization";
payloadDictionary[@"scope"] = scope;
NSData *data = [NSJSONSerialization dataWithJSONObject:payloadDictionary options:kNilOptions error:&error];
[urlRequest setHTTPBody:data];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
String type = "edit";
String scope = "organization";
graphClient.me().drive().items("{item-id}")
.createLink(DriveItemCreateLinkParameterSet
.newBuilder()
.withType(type)
.withScope(scope)
.withExpirationDateTime(null)
.withPassword(null)
.withMessage(null)
.withRetainInheritedPermissions(null)
.build())
.buildRequest()
.post();
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
応答
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": "123ABC",
"roles": ["write"],
"link": {
"type": "edit",
"scope": "organization",
"webUrl": "https://contoso-my.sharepoint.com/personal/ellen_contoso_com/...",
"application": {
"id": "1234",
"displayName": "Sample Application"
},
},
}
埋め込み可能なリンクを作成する
リンクの種類で embed を使用する場合、返される webUrl は <iframe> HTML 要素に埋め込むことができます。埋め込みリンクが作成されると、webHtml プロパティに <iframe> がコンテンツをホストするための HTML コードが含まれます。
注: 埋め込みリンクは、OneDrive 個人用でのみサポートされます。
要求
POST /me/drive/items/{item-id}/createLink
Content-Type: application/json
{
"type": "embed"
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var type = "embed";
await graphClient.Me.Drive.Items["{driveItem-id}"]
.CreateLink(type,null,null,null,null,null)
.Request()
.PostAsync();
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
const options = {
authProvider,
};
const client = Client.init(options);
const permission = {
type: 'embed'
};
await client.api('/me/drive/items/{item-id}/createLink')
.post(permission);
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/v1.0/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/drive/items/{item-id}/createLink"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSMutableDictionary *payloadDictionary = [[NSMutableDictionary alloc] init];
NSString *type = @"embed";
payloadDictionary[@"type"] = type;
NSData *data = [NSJSONSerialization dataWithJSONObject:payloadDictionary options:kNilOptions error:&error];
[urlRequest setHTTPBody:data];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
String type = "embed";
graphClient.me().drive().items("{item-id}")
.createLink(DriveItemCreateLinkParameterSet
.newBuilder()
.withType(type)
.withScope(null)
.withExpirationDateTime(null)
.withPassword(null)
.withMessage(null)
.withRetainInheritedPermissions(null)
.build())
.buildRequest()
.post();
SDK をプロジェクトに追加し、authProvider インスタンスを作成する 方法の詳細については、SDK のドキュメントを参照してください 。
応答
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": "123ABC",
"roles": ["read"],
"link": {
"type": "embed",
"webHtml": "<IFRAME src=\"https://onedrive.live.com/...\"></IFRAME>",
"webUrl": "https://onedrive.live.com/...",
"application": {
"id": "1234",
"displayName": "Sample Application"
},
}
}
組織に既定の有効期限ポリシーが適用されている場合を除き、このアクションを使用して作成されたリンクに期限はありません。
リンクはそのアイテムの共有アクセス許可に表示され、アイテムの所有者はそれを削除できます。
リンクは、そのアイテムがチェックアウトされない限り、常にアイテムの現在のバージョンをポイントします (SharePoint の場合のみ)。