此请求的授权已被拒绝-Microsoft Graph API,Microsoft Booking API [英] Authorization has been denied for this request -- Microsoft Graph API, Microsoft Booking API

查看:139
本文介绍了此请求的授权已被拒绝-Microsoft Graph API,Microsoft Booking API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的ASP.Net Web应用程序,该应用程序由.aspx Web组成,并由Azure托管为云服务.在我的应用程序中,没有用户登录.
我想连接Microsoft Graph API,并使用Microsoft Bookings API在我的主页加载中获取BookingBusiness集合,而无需用户登录.我目前正在使用Azure模拟器在桌面上调试Web应用.
我拥有与Microsoft帐户(XXXXX@microsoft.com)相关联的ofiice 365高级帐户访问权限,并且已经通过预订工具(https://outlook.office.com/owa/?path)使用我的v-别名创建了预订业务. =/bookings.
我已在同一租户中的AAD中注册了一个应用程序,并具有所有必需的权限,并在代码中提供了Cliend ID和密码以获取访问令牌.我正在使用客户端凭据"授予流来获取访问令牌并尝试调用预订API. 我能够获得访问令牌,,但是当代码尝试获取预订企业列表时,它给出了以下异常.

DataServiceClientException:{
  错误":{
    " code":",
    " message":此请求的授权已被拒绝.",
    "innerError":{
      "request-id":"d0ac6470-9aae-4cc2-9bf3-ac83e700fd6a",
      " date" ;:" 2018-09-03T08:38:29"
    }
  }
}

代码和注册的应用程序设置详细信息在以下屏幕截图中.

I have a simple ASP.Net web application consist of .aspx web from hosted on azure as cloud service. In my application there is no user login.
I want to connect with Microsoft Graph API and and to use Microsoft Bookings API to get the BookingBusiness collection on my home page load without user login. I am currently debugging my web app on my desktop using Azure emulator.
I have the ofiice 365 premium account access assoiciated with my microsoft account (XXXXX@microsoft.com) and I had created a Booking business using my v- alias through Booking tools (https://outlook.office.com/owa/?path=/bookings).
I registered an app in AAD in the same tenant with all required permission and provided the Cliend Id and secret in the code to get the access token. I am using Client credentials Grant flow to get the access token and try to invoke the booking API. I am able to get the access token, but when the code try to get the the list of booking businesses it is giving below exception.

DataServiceClientException: {
  "error": {
    "code": "",
    "message": "Authorization has been denied for this request.",
    "innerError": {
      "request-id": "d0ac6470-9aae-4cc2-9bf3-ac83e700fd6a",
      "date": "2018-09-03T08:38:29"
    }
  }
}

The code and registered app setting details are in below screen shot.


        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

GraphService.cs

// ---------------------------------------------------------------------------
// <copyright file="GraphService.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// ---------------------------------------------------------------------------

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());
        }

        /// <summary>
        /// Gets or sets the odata.maxpagesize preference header.
        /// </summary>
        /// <remarks>
        /// Using the Prefer header we can control the resulting page size of certain operations,
        /// in particular of GET bookingBusinesses(id)/appointments and bookingBusinesses(id)/customers.
        /// </remarks>
        public int? MaxPageSize
        {
            get;
            set;
        } = null;

        /// <summary>
        /// Gets or sets the odata.continue-on-error preference header.
        /// </summary>
        /// <remarks>
        /// Using the Prefer header we can control if batch operations stop or continue on error.
        /// </remarks>
        public bool ContinueOnError
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the web proxy to use when sending requests.
        /// </summary>
        public IWebProxy WebProxy
        {
            get;
            set;
        }

        partial void OnContextCreated()
        {
            // Default to send only the properties that were set on a data object
            this.EntityParameterSendOption = EntityParameterSendOption.SendOnlySetProperties;

            // Allows new results to override cached results, if the object is not changed.
            this.MergeOption = MergeOption.PreserveChanges;

            if (this.BaseUri.AbsoluteUri[this.BaseUri.AbsoluteUri.Length - 1] != '/')
            {
                throw new ArgumentException("BaseUri must end with '/'");
            }

            this.BuildingRequest += (s, e) => e.Headers.Add("client-request-id", Guid.NewGuid().ToString());

            this.SendingRequest2 += (s, e) =>
                {
                    var requestMessage = e.RequestMessage as HttpWebRequestMessage;
                    if (requestMessage != null)
                    {
                        var preferenceHeader = new ODataRequestOnHttpWebRequest(requestMessage.HttpWebRequest).PreferHeader();
                        preferenceHeader.MaxPageSize = this.MaxPageSize;
                        preferenceHeader.ContinueOnError = this.ContinueOnError;

                        requestMessage.HttpWebRequest.Proxy = this.WebProxy;
                    }
                };
        }
    }
}



推荐答案

The error message "Authorization has been denied for this request" which you are getting is the specified credentials do not have sufficient privileges to make this request. The request is denied due to insufficient privileges. You may refer the following document link to know information about the error for troubleshooting.

参考:- 为Microsoft Graph Call找到正确的权限.

Ref:- Error codes and error handling and Finding the correct permissions for Microsoft Graph Call.

--------------- -------------------------------------------------- ------------------------------

如果此答案有帮助,请单击标记为答案"或投票.要提供有关您的论坛体验的其他反馈,请单击

这篇关于此请求的授权已被拒绝-Microsoft Graph API,Microsoft Booking API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆