microsoft-graph-api - 使用第一方应用程序调用 Microsoft Graph API 失败,权限不足,无法完成对“SubscribedSkus API”的操作
问题描述
调用 microsoft graph API https://graph.microsoft.com/v1.0/subscribedSkus失败,出现
“code”:“Authorization_RequestDenied”,“message”:“Insufficient rights to complete the operation.”,如果我们创建一个租户中非管理员的新用户。但是在使用管理员用户调用它时它工作得很好。即使它适用于租户中的任何微软用户。
这是我曾经尝试过的以下代码。
public static async Task TestAadGraph()
{
// using obo token of the user.
var graphToken = await GetTokenAsync(UserId, Token, "https://graph.microsoft.com");
var aadGraphClient = new AadGraphClient(new HttpClient());
var licenseResponse = await aadGraphClient.GetTenantLicenseDetailAsync(graphToken);
foreach (var license in licenseResponse)
{
Console.WriteLine("Sku ID: {0}", license.SkuId);
Console.WriteLine("Sku Part Number: {0}", license.SkuPartNumber);
foreach (var plan in license.ServicePlans)
{
Console.WriteLine("Plan Id: {0}", plan.ServicePlanId);
Console.WriteLine("Plan Name: {0}", plan.ServicePlanName);
}
}
}
public async Task<SubscribedSku[]> GetTenantLicenseDetailAsync(string accessToken)
{
var request = new RequestMessage
{
BearerToken = accessToken,
Endpoint = new Uri("http://graph.microsoft.com/v1.0/subscribedSkus"),
};
var response = await _httpClient.FetchAsync<SubscribedSkusResponse>(request);
return response.Value;
}
public static async Task<T> FetchAsync<T>(this HttpClient httpClient, RequestMessage request, Action<HttpResponseMessage, string> responseCallback) where T : class
{
request.Method = request.Method ?? HttpMethod.Get;
request.MediaType = request.MediaType ?? "application/json";
using (HttpRequestMessage message = new HttpRequestMessage(request.Method,
UrlHelper.AppendParameters(request.Params, request.Endpoint)))
{
if (!string.IsNullOrEmpty(request.BearerToken))
{
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer",
request.BearerToken);
}
if (request.Headers != null)
{
foreach (KeyValuePair<string, string> header in request.Headers)
{
message.Headers.Add(header.Key, header.Value);
}
}
if (!string.IsNullOrEmpty(request.Content))
{
message.Content = new StringContent(request.Content, Encoding.UTF8,
request.MediaType);
}`
using (HttpResponseMessage response = await httpClient.SendAsync(message))
{
string json = await response.Content.ReadAsStringAsync();
if (responseCallback != null)
{
responseCallback?.Invoke(response, json);
}
if (response.IsSuccessStatusCode)
{
if (predictor != null)
{
json = predictor(JToken.Parse(json)).ToString();
}
return JsonConvert.DeserializeObject<T>(json);
}
else
{
throw new WebRequestException(response, json);
}
}
}
}
解决方案
首先,在Microsoft Graph Explorer中尝试对新创建的用户进行相同的调用,以查看是否存在相同的场景。如果没有,则表示新用户没有任何问题。
然后调试你的代码并将graphToken复制到https://jwt.io/,看看Decoded结果是否具有所需的Delegated权限之一:<code>Organization.Read.All、Directory.Read.All、Organization.ReadWrite。全部,Directory.ReadWrite.All,Directory.AccessAsUser.All。请注意,“upn”应该是新创建用户的用户名。
如果所需的权限不存在,则需要在 Azure AD 应用程序中分配权限。请参阅API 权限。
推荐阅读
- javascript - 设置嵌套数组的状态
- google-apps-script - Google Apps 脚本 - 检查表是否存在,如果存在则将其删除,检查删除作业是否完成
- c# - C#:SQL - 一次一个查询与命令文本中的多个查询?
- javascript - 父级重新渲染不更新子道具
- r - ggplot2 用于具有两个相同刻度标签的条形图
- javascript - 服务器和客户端中的类名水合不匹配,(警告:Prop `className` 与服务器和客户端不匹配)
- cassandra - 将 DNS 故障转移用作多 DC 故障转移策略吗?
- java - 忽略类型类
- javascript - 过去滚动时使元素固定
- automated-tests - 在 Microsoft Dynamics AX 2009 上进行自动化测试?