(委托的权限授予) 创建 oAuth2PermissionGrant
本文内容
命名空间:microsoft.graph
重要
Microsoft Graph /beta
版本下的 API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。
创建委托的权限授予,由 oAuth2PermissionGrant 对象表示。
委托的权限授予授权客户端服务主体 (表示客户端应用程序) 访问资源服务主体, (代表已登录用户的 API) ,以授予的委派权限所限制的访问级别。
此 API 可用于以下国家级云部署 。
全局服务
美国政府 L4
美国政府 L5 (DOD)
由世纪互联运营的中国
✅
✅
✅
✅
权限
为此 API 选择标记为最低特权的权限。 只有在应用需要它时 ,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型 。 要了解有关这些权限的详细信息,请参阅 权限参考 。
权限类型
最低特权权限
更高特权权限
委派(工作或学校帐户)
DelegatedPermissionGrant.ReadWrite.All
Directory.ReadWrite.All
委派(个人 Microsoft 帐户)
不支持。
不支持。
应用程序
DelegatedPermissionGrant.ReadWrite.All
Directory.ReadWrite.All
HTTP 请求
POST /oauth2PermissionGrants
名称
类型
说明
Authorization
string
持有者 {token}。 必填。
请求正文
在请求正文中,提供 oAuth2PermissionGrant 对象的 JSON 表示形式。
下表列出了创建 oAuth2PermissionGrant 时所需的属性。
属性
类型
说明
clientId
字符串
对象 ID (不是 应用程序客户端服务主体 的 appId ) ,应用程序有权在访问 API 时代表已登录用户进行操作。 必填。
consentType
String
指示是授予客户端应用程序模拟所有用户或仅特定用户的授权。 AllPrincipals 指示模拟所有用户的授权。 主体 指示模拟特定用户的授权。 管理员可以代表所有用户授予同意。 在某些情况下,对于某些委派的权限,非管理员用户可能有权代表自己同意。 必填。
principalId
String
如果 consentType 为 Principal ,则代表客户端访问资源的用户的 ID 。 如果 consentType 为 AllPrincipals, 则此值为 null。 当 consentType 为 Principal 时是必需的。
resourceId
String
有权访问的资源服务主体 的 ID 。 这将标识客户端有权代表已登录用户尝试调用的 API。
范围
String
委派权限的声明值的空格分隔列表,该列表应包含在资源应用程序的访问令牌中, (API) 。 例如,openid User.Read GroupMember.Read.All
。 每个声明值应与 API 定义的委托权限之一的值 字段匹配,该权限在资源服务主体 的 publishedPermissionScopes 属性中列出。 长度不得超过 3850 个字符。
startTime
DateTimeOffset
目前,将忽略开始时间值,但需要一个值。 必填。
expiryTime
DateTimeOffset
目前,结束时间值被忽略,但需要一个值。 必填。
响应
如果成功,此方法在响应正文中返回 200 系列响应代码和新的 oAuth2PermissionGrant 对象。
示例
请求
POST https://graph.microsoft.com/beta/oauth2PermissionGrants
Content-Type: application/json
{
"clientId": "ef969797-201d-4f6b-960c-e9ed5f31dab5",
"consentType": "AllPrincipals",
"resourceId": "943603e4-e787-4fe9-93d1-e30f749aae39",
"scope": "DelegatedPermissionGrant.ReadWrite.All",
"startTime": "2022-03-17T00:00:00Z",
"expiryTime": "2023-03-17T00:00:00Z"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Models;
var requestBody = new OAuth2PermissionGrant
{
ClientId = "ef969797-201d-4f6b-960c-e9ed5f31dab5",
ConsentType = "AllPrincipals",
ResourceId = "943603e4-e787-4fe9-93d1-e30f749aae39",
Scope = "DelegatedPermissionGrant.ReadWrite.All",
StartTime = DateTimeOffset.Parse("2022-03-17T00:00:00Z"),
ExpiryTime = DateTimeOffset.Parse("2023-03-17T00:00:00Z"),
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Oauth2PermissionGrants.PostAsync(requestBody);
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc-beta oauth2-permission-grants create --body '{\
"clientId": "ef969797-201d-4f6b-960c-e9ed5f31dab5",\
"consentType": "AllPrincipals",\
"resourceId": "943603e4-e787-4fe9-93d1-e30f749aae39",\
"scope": "DelegatedPermissionGrant.ReadWrite.All",\
"startTime": "2022-03-17T00:00:00Z",\
"expiryTime": "2023-03-17T00:00:00Z"\
}\
'
import (
"context"
"time"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
//other-imports
)
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
requestBody := graphmodels.NewOAuth2PermissionGrant()
clientId := "ef969797-201d-4f6b-960c-e9ed5f31dab5"
requestBody.SetClientId(&clientId)
consentType := "AllPrincipals"
requestBody.SetConsentType(&consentType)
resourceId := "943603e4-e787-4fe9-93d1-e30f749aae39"
requestBody.SetResourceId(&resourceId)
scope := "DelegatedPermissionGrant.ReadWrite.All"
requestBody.SetScope(&scope)
startTime , err := time.Parse(time.RFC3339, "2022-03-17T00:00:00Z")
requestBody.SetStartTime(&startTime)
expiryTime , err := time.Parse(time.RFC3339, "2023-03-17T00:00:00Z")
requestBody.SetExpiryTime(&expiryTime)
oauth2PermissionGrants, err := graphClient.Oauth2PermissionGrants().Post(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
OAuth2PermissionGrant oAuth2PermissionGrant = new OAuth2PermissionGrant();
oAuth2PermissionGrant.setClientId("ef969797-201d-4f6b-960c-e9ed5f31dab5");
oAuth2PermissionGrant.setConsentType("AllPrincipals");
oAuth2PermissionGrant.setResourceId("943603e4-e787-4fe9-93d1-e30f749aae39");
oAuth2PermissionGrant.setScope("DelegatedPermissionGrant.ReadWrite.All");
OffsetDateTime startTime = OffsetDateTime.parse("2022-03-17T00:00:00Z");
oAuth2PermissionGrant.setStartTime(startTime);
OffsetDateTime expiryTime = OffsetDateTime.parse("2023-03-17T00:00:00Z");
oAuth2PermissionGrant.setExpiryTime(expiryTime);
OAuth2PermissionGrant result = graphClient.oauth2PermissionGrants().post(oAuth2PermissionGrant);
const options = {
authProvider,
};
const client = Client.init(options);
const oAuth2PermissionGrant = {
clientId: 'ef969797-201d-4f6b-960c-e9ed5f31dab5',
consentType: 'AllPrincipals',
resourceId: '943603e4-e787-4fe9-93d1-e30f749aae39',
scope: 'DelegatedPermissionGrant.ReadWrite.All',
startTime: '2022-03-17T00:00:00Z',
expiryTime: '2023-03-17T00:00:00Z'
};
await client.api('/oauth2PermissionGrants')
.version('beta')
.post(oAuth2PermissionGrant);
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Models\OAuth2PermissionGrant;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new OAuth2PermissionGrant();
$requestBody->setClientId('ef969797-201d-4f6b-960c-e9ed5f31dab5');
$requestBody->setConsentType('AllPrincipals');
$requestBody->setResourceId('943603e4-e787-4fe9-93d1-e30f749aae39');
$requestBody->setScope('DelegatedPermissionGrant.ReadWrite.All');
$requestBody->setStartTime(new \DateTime('2022-03-17T00:00:00Z'));
$requestBody->setExpiryTime(new \DateTime('2023-03-17T00:00:00Z'));
$result = $graphServiceClient->oauth2PermissionGrants()->post($requestBody)->wait();
Import-Module Microsoft.Graph.Beta.Identity.SignIns
$params = @{
clientId = "ef969797-201d-4f6b-960c-e9ed5f31dab5"
consentType = "AllPrincipals"
resourceId = "943603e4-e787-4fe9-93d1-e30f749aae39"
scope = "DelegatedPermissionGrant.ReadWrite.All"
startTime = [System.DateTime]::Parse("2022-03-17T00:00:00Z")
expiryTime = [System.DateTime]::Parse("2023-03-17T00:00:00Z")
}
New-MgBetaOauth2PermissionGrant -BodyParameter $params
from msgraph import GraphServiceClient
from msgraph.generated.models.o_auth2_permission_grant import OAuth2PermissionGrant
graph_client = GraphServiceClient(credentials, scopes)
request_body = OAuth2PermissionGrant(
client_id = "ef969797-201d-4f6b-960c-e9ed5f31dab5",
consent_type = "AllPrincipals",
resource_id = "943603e4-e787-4fe9-93d1-e30f749aae39",
scope = "DelegatedPermissionGrant.ReadWrite.All",
start_time = "2022-03-17T00:00:00Z",
expiry_time = "2023-03-17T00:00:00Z",
)
result = await graph_client.oauth2_permission_grants.post(request_body)
响应
HTTP/1.1 201 Created
Content-Type: application/json
{
"@odata.context": "https://graph.microsoft.com/beta/$metadata#oauth2PermissionGrants/$entity",
"clientId": "ef969797-201d-4f6b-960c-e9ed5f31dab5",
"consentType": "AllPrincipals",
"expiryTime": "2023-03-17T00:00:00Z",
"id": "l5eW7x0ga0-WDOntXzHateQDNpSH5-lPk9HjD3Sarjk",
"principalId": null,
"resourceId": "943603e4-e787-4fe9-93d1-e30f749aae39",
"scope": "DelegatedPermissionGrant.ReadWrite.All",
"startTime": "2022-03-17T00:00:00Z"
}