【发布时间】:2018-09-03 12:36:47
【问题描述】:
我有一个简单的 ASP.Net Web 应用程序,其中包含托管在 azure 上的 .aspx Web 作为云服务。在我的应用程序中没有用户登录。 我想连接 Microsoft Graph API 并使用 Microsoft Bookings API 在我的主页加载时获取 BookingBusiness 集合,而无需用户登录。我目前正在使用 Azure 模拟器在我的桌面上调试我的 Web 应用程序。 我拥有与我的 microsoft 帐户 (v-sheeal@microsoft.com) 关联的 office 365 高级帐户访问权限,并且我通过预订工具 (https://outlook.office.com/owa/?path=/bookings) 使用我的 v-别名创建了预订业务。 我在同一租户的 AAD 中注册了一个具有所有必需权限的应用程序,并在代码中提供了客户端 ID 和密码以获取访问令牌。我正在使用客户端凭据授予流程来获取访问令牌并尝试调用预订 API。我能够获取访问令牌,但是当代码尝试获取预订业务列表时,它给出了以下异常。
DataServiceClientException: { “错误”: { “代码”: ””, "message": "此请求的授权已被拒绝。", “内部错误”:{ “请求 ID”:“d0ac6470-9aae-4cc2-9bf3-ac83e700fd6a”, “日期”:“2018-09-03T08:38:29” } } }
代码和注册的应用设置详细信息在下面的屏幕截图中。 .aspx.cs
private static async Task<AuthenticationResult> AcquireToken()
{
var tenant = "microsoft.onmicrosoft.com";
//"yourtenant.onmicrosoft.com";
var resource = "https://graph.microsoft.com/";
var instance = "https://login.microsoftonline.com/";
var clientID = "7389d0b8-1611-4ef9-a01f-eba4c59a6427";
var secret = "mxbPBS10|[#!mangJHQF791";
var authority = $"{instance}{tenant}";
var authContext = new AuthenticationContext(authority);
var credentials = new ClientCredential(clientID, secret);
var authResult = await authContext.AcquireTokenAsync(resource,
credentials);
return authResult;
}
protected void MSBooking()
{
var authenticationContext = new
AuthenticationContext(GraphService.DefaultAadInstance,
TokenCache.DefaultShared);
var authenticationResult = AcquireToken().Result;
var graphService = new GraphService(
GraphService.ServiceRoot,
() => authenticationResult.CreateAuthorizationHeader());
// Get the list of booking businesses that the logged on user can see.
var bookingBusinesses = graphService.BookingBusinesses; ----- this
line throwing an exception "Authorization has been denied for
this request."
}
GraphService.cs
namespace Microsoft.Bookings.Client
{
using System;
using System.Net;
using Microsoft.OData;
using Microsoft.OData.Client;
public partial class GraphService
{
/// <summary>
/// The resource identifier for the Graph API.
/// </summary>
public const string ResourceId = "https://graph.microsoft.com/";
/// <summary>
/// The default AAD instance to use when authenticating.
/// </summary>
public const string DefaultAadInstance =
"https://login.microsoftonline.com/common/";
/// <summary>
/// The default v1 service root
/// </summary>
public static readonly Uri ServiceRoot = new
Uri("https://graph.microsoft.com/beta/");
/// <summary>
/// Initializes a new instance of the <see
cref="BookingsContainer"/> class.
/// </summary>
/// <param name="serviceRoot">The service root.</param>
/// <param name="getAuthenticationHeader">A delegate that returns
the authentication header to use in each request.</param>
public GraphService(Uri serviceRoot, Func<string>
getAuthenticationHeader)
: this(serviceRoot)
{
this.BuildingRequest += (s, e) => e.Headers.Add("Authorization",
getAuthenticationHeader());
}
}
【问题讨论】:
标签: microsoft-graph-api microsoft-graph-booking