我有一个简单的ASP.Net Web应用程序,该应用程序由.aspx Web组成,并由Azure托管为云服务。在我的应用程序中,没有用户登录。 我想连接Microsoft Graph API,并使用Microsoft Bookings API在我的主页加载中获取BookingBusiness集合,而无需用户登录。我目前正在使用Azure模拟器在桌面上调试Web应用程序。 我拥有与Microsoft帐户(v-sheeal@microsoft.com)关联的办公365高级帐户访问权限,并且已经通过预订工具(https://outlook.office.com/owa/?path=/bookings)使用我的v-别名创建了预订业务。 我在同一租户中的AAD中注册了一个具有所有必需权限的应用程序,并在代码中提供了Cliend ID和密码以获取访问令牌。我正在使用“客户凭证”授予流来获取访问令牌并尝试调用预订API。我能够获得访问令牌,但是当代码尝试获取预订企业的列表时,它给出了以下异常。
DataServiceClientException:{ “错误”:{ “ code”:“”, “ message”:“该请求的授权已被拒绝。”, “ innerError”:{ “ request-id”:“ d0ac6470-9aae-4cc2-9bf3-ac83e700fd6a”, “ date”:“ 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());
}
}
答案 0 :(得分:0)
根据您的描述,我假设您要使用Microsoft Bookings API。
根据您提供的图像,您的代码中缺少定义范围,并且授权不正确。
我们可以将文档审阅到get an Access Token without a user。