(委托的权限授予) 创建 oAuth2PermissionGrant
本文内容
命名空间:microsoft.graph
创建由 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 表示形式。
响应
如果成功,此方法在响应正文中返回 200 系列响应代码和新的 oAuth2PermissionGrant 对象。 下表列出了创建 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 定义的委托权限之一的值 字段匹配,该权限在资源服务主体 的 oauth2PermissionScopes 属性中列出。 长度不得超过 3850 个字符。
示例
请求
POST https://graph.microsoft.com/v1.0/oauth2PermissionGrants
Content-Type: application/json
{
"clientId": "ef969797-201d-4f6b-960c-e9ed5f31dab5",
"consentType": "AllPrincipals",
"resourceId": "943603e4-e787-4fe9-93d1-e30f749aae39",
"scope": "DelegatedPermissionGrant.ReadWrite.All"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Models;
var requestBody = new OAuth2PermissionGrant
{
ClientId = "ef969797-201d-4f6b-960c-e9ed5f31dab5",
ConsentType = "AllPrincipals",
ResourceId = "943603e4-e787-4fe9-93d1-e30f749aae39",
Scope = "DelegatedPermissionGrant.ReadWrite.All",
};
// 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);
有关如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档 。
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc oauth2-permission-grants create --body '{\
"clientId": "ef969797-201d-4f6b-960c-e9ed5f31dab5",\
"consentType": "AllPrincipals",\
"resourceId": "943603e4-e787-4fe9-93d1-e30f749aae39",\
"scope": "DelegatedPermissionGrant.ReadWrite.All"\
}\
'
有关如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档 。
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-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)
oauth2PermissionGrants, err := graphClient.Oauth2PermissionGrants().Post(context.Background(), requestBody, nil)
有关如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档 。
// 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");
OAuth2PermissionGrant result = graphClient.oauth2PermissionGrants().post(oAuth2PermissionGrant);
有关如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档 。
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'
};
await client.api('/oauth2PermissionGrants')
.post(oAuth2PermissionGrant);
有关如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档 。
<?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');
$result = $graphServiceClient->oauth2PermissionGrants()->post($requestBody)->wait();
有关如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档 。
Import-Module Microsoft.Graph.Identity.SignIns
$params = @{
clientId = "ef969797-201d-4f6b-960c-e9ed5f31dab5"
consentType = "AllPrincipals"
resourceId = "943603e4-e787-4fe9-93d1-e30f749aae39"
scope = "DelegatedPermissionGrant.ReadWrite.All"
}
New-MgOauth2PermissionGrant -BodyParameter $params
有关如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档 。
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",
)
result = await graph_client.oauth2_permission_grants.post(request_body)
有关如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档 。
响应
HTTP/1.1 201 Created
Content-Type: application/json
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#oauth2PermissionGrants/$entity",
"clientId": "ef969797-201d-4f6b-960c-e9ed5f31dab5",
"consentType": "AllPrincipals",
"id": "l5eW7x0ga0-WDOntXzHateQDNpSH5-lPk9HjD3Sarjk",
"principalId": null,
"resourceId": "943603e4-e787-4fe9-93d1-e30f749aae39",
"scope": "DelegatedPermissionGrant.ReadWrite.All"
}