![](/img/trans.png)
[英]Cannot operate on variables of type directoryObject, MS Graph API result
[英]MS graph API Oauth2PermissionGrants cannot grant roles
使用下面的代码,我可以第一次批准给予管理员同意。 但是当我第二次调用代码时。 代码返回错误。
例子:
我可以在第一个请求时授予User.Read.All
访问权限。 但是,当我想为第二个角色AccessReview.Read.All
授予管理员同意时,请求给出以下错误
代码
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create("Client ID")
.WithClientSecret("Client Secret")
.WithTenantId("Tenant ID")
.Build();
string scopes = "https://graph.microsoft.com/.default";
ClientCredentialProvider authProvider = new ClientCredentialProvider(app, scopes);
Beta.GraphServiceClient graphClient = new Beta.GraphServiceClient(authProvider);
Beta.OAuth2PermissionGrant test = new Beta.OAuth2PermissionGrant { ClientId = model.clientId, ConsentType = model.consentType, ExpiryTime = model.expiryTime, ResourceId = model.resourceId, Scope = model.scope };
var response = await graphClient.Oauth2PermissionGrants
.Request()
.AddAsync(test);
return response.ToString();
错误
Status Code: Conflict
Microsoft.Graph.ServiceException: Code: Request_MultipleObjectsWithSameKeyValue
Message: Permission entry already exists.
Inner error:
AdditionalData:
request-id: b9e44bc2-7588-4390-a3ca-9abdc213d930
date: 2020-04-23T19:43:03
ClientRequestId: b9e44bc2-7588-4390-a3ca-9abdc213d930
您不能两次添加相同的权限授予,正如错误消息所说, permission entry already exists
。 如果要更新权限授予,可以使用 update 方法:
var oAuth2PermissionGrant = new OAuth2PermissionGrant
{
Scope = "scope-value"
};
await graphClient.OAuth2Permissiongrants["{id}"]
.Request()
.UpdateAsync(oAuth2PermissionGrant);
参考:
我发现了这个问题。 图 API 为 Oauth 创建单个 object 根据同意类型授予权限。 因此,对于第一个请求,调用 post 请求以授予访问权限。 但是对于第二个请求,使用 patch 命令更新 object
var oAuth2PermissionGrant = new OAuth2PermissionGrant
{
Scope = "scope-value"
};
await graphClient.OAuth2Permissiongrants["{id}"]
.Request()
.UpdateAsync(oAuth2PermissionGrant);
注意:在 patch 命令中添加您已授予访问权限并希望授予访问权限的范围。 如果您仅添加要授予访问权限的 scope,则先前的范围授予访问权限将被撤销,并且只有新角色将具有授予访问权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.