“ Microsoft图形”中的“ Microsoft Bookings api”是否支持“资源所有者凭据授予”访问令牌?

时间:2019-06-25 22:37:19

标签: microsoft-graph microsoft-graph-sdks

我将使用预订API来获取一个特定用户帐户的bookingBusiness列表: 一种。帐户被确认为Office 365商业高级许可证用户 b。帐户以隐式流程(登录页面方式)登录后,可以通过“ Graph Exploer”调用预订api以获取BookingBusiness列表。

当我尝试从“资源所有者流”获取访问令牌(成功获取访问令牌)并用于带有Graph Beta SDK(Microsoft.Graph.Beta v0.6预览版)的预订Api调用时,GET调用永远不会返回,则没有结果集或错误返回以完成调用。

public static class GraphHelper
    {

        public static void GetBusinessListTest()
        {
            var graphClient = GetAuthenticatedClient();
            var bookingBusinesses = graphClient.BookingBusinesses.Request();
            // the calling below never return a result back or throw error code
            var res=  bookingBusinesses.GetAsync().Result;
           return;
        }

        private static GraphServiceClient GetAuthenticatedClient()
        {
            var authProvider = GetAuthProvider();
            return new GraphServiceClient(authProvider);
        }


        private static IAuthenticationProvider GetAuthProvider()
        {
            string tenantId = "xxxx-xxxx-xxxx-xxxx-xxxx";
            string authority = $"https://login.microsoftonline.com/{tenantId}";

            //scopes string - Bookings.Manage.All User.Read
            string[] scopes = graphScopes.Split(' ');
            IPublicClientApplication app;
            //GET access token from user login and credentials
            app = PublicClientApplicationBuilder.Create(appId)
               .WithTenantId(tenantId)
                  .WithAuthority(authority)
                  .Build();
            var accounts = app.GetAccountsAsync().Result;

            AuthenticationResult result = null;
            if (accounts.Any())
            {
                var acct = accounts.FirstOrDefault();
                result = app.AcquireTokenSilent(scopes, acct)
                                  .ExecuteAsync().Result;
            }
            else
            {
                try
                {
                    var securePassword = new SecureString();
                    foreach (char c in "login user password")        // you should fetch the password
                        securePassword.AppendChar(c);  // keystroke by keystroke

                    result = app.AcquireTokenByUsernamePassword(scopes,"loginuser@EmailAddress",securePassword).ExecuteAsync().Result;

                }
                catch (MsalException ex)
                {
                    var ss = ex.Message;
                }
            }

           //assign access token to Grapth Deletegate access provider HTTP request header - bearer token
            var authenticationProvider = new Microsoft.Graph.DelegateAuthenticationProvider(
                async (requestMessage) =>
                  {

                      requestMessage.Headers.Authorization =
                         new AuthenticationHeaderValue("Bearer", result.AccessToken);
                  });
            return authenticationProvider;

        }
    }
// call to booking Businesses https://graph.microsoft.com/beta/bookingBusinesses
 public IGraphServiceBookingBusinessesCollectionRequest Request(IEnumerable<Option> options)
        {
            return new GraphServiceBookingBusinessesCollectionRequest(this.RequestUrl, this.Client, options);
        }

public async System.Threading.Tasks.Task<IGraphServiceBookingBusinessesCollectionPage> GetAsync(CancellationToken cancellationToken)
        {
            this.Method = "GET";

// *********the code execution is eactly stucking in the line below, a normal HTTP Get Call from Base Request**********
            var response = await this.SendAsync<GraphServiceBookingBusinessesCollectionResponse>(null, cancellationToken).ConfigureAwait(false);
            if (response != null && response.Value != null && response.Value.CurrentPage != null)
            {
                if (response.AdditionalData != null)
                {
                    object nextPageLink;
                    response.AdditionalData.TryGetValue("@odata.nextLink", out nextPageLink);

                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        response.Value.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }

                    // Copy the additional data collection to the page itself so that information is not lost
                    response.Value.AdditionalData = response.AdditionalData;
                }

                return response.Value;
            }

            return null;
        }

我希望BookingBusinesses列表返回或返回错误代码并返回错误响应。

如果深度预订API可以支持从“资源所有者凭证”流授予的访问令牌,是否有任何专家可以帮助回答?如果是的话,我的努力有什么问题

任何建议或回复将不胜感激

---------------更新---------- 在Azure AD中,应用程序已设置适当的设置以允许资源所有者访问类型 application authentication setting screenshot

0 个答案:

没有答案