【问题标题】:Microsoft Graph API - Manage Booking API Authorization has been denied for this requestMicrosoft Graph API - 管理预订 API 对此请求的授权已被拒绝
【发布时间】: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


    【解决方案1】:

    根据您的描述,我假设您想使用 Microsoft Bookings API。

    根据您提供的图像,您的代码中缺少定义范围,权限不正确。

    我们可以审阅文件到get an Access Token without a user

    【讨论】:

    • 感谢您的回复。您能否让我知道范围和权限的值应该是什么?我浏览了您提到的文档,但我无法找到我需要设置的正确值。
    • 作为文档,范围应该是'graph.microsoft.com/.default'。并且此值会通知 v2.0 端点您为应用配置的所有应用权限
    • 谢谢,我将范围更改为 'graph.microsoft.com/.default' 和 'graph.microsoft.com/beta' 但它仍然未能通过身份验证并给出以下错误,如 Fiddler 中所示。
    • {"error":"invalid_resource","error_description":"AADSTS50001: 在名为 microsoft.onmicrosoft.com 的租户中找不到名为 graph.microsoft.com/beta 的应用程序。如果应用程序具有租户管理员未安装或租户中的任何用户同意。你可能已将身份验证请求发送到错误的租户。\r\n跟踪 ID: edc54570-5-f14a00\r\n相关 ID: 8d81c1e8- 035878657\r\n时间戳:2018-09-12 07:29:18Z","error_codes":[501],"timestamp":"2018-09-12 07:29:18Z","trace_id":"edc- fcfa00","correlation_id":"8d1e88657"}.
    • 现在我更改了编码方法并使用本机应用程序方法,但是是的,我们需要登录。现在我可以连接 Booking API,下一个问题是,如何将 OData JSON 转换为 Microsoft.Bookings.Client BookingBusiness 类对象?我的 Json PayLoad 是。
    猜你喜欢
    • 2016-06-25
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2019-07-17
    • 2019-03-02
    • 1970-01-01
    相关资源
    最近更新 更多