无法创建用于访问Office365日历的Discovery Client对象 [英] Unable to create Discovery Client object for Accessing Office365 calendar

查看:129
本文介绍了无法创建用于访问Office365日历的Discovery Client对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Office 365日历集成到我的应用程序中. 我在做这样的

I need to integrate Office 365 Calendar into my application. I'm doing Like this

    public static string ClientID = SettingsHelper.ClientId;
    public static Uri _returnUri = new Uri(SettingsHelper.RedirectUri);
    public const string CommonAuthority = "https://login.windows.net/Common";
    public const string DiscoveryResourceId = "https://api.office.com/discovery/";

    public static AuthenticationContext _authenticationContext { get; set; }
    private static async Task<string> GetTokenHelperAsync(AuthenticationContext context, string resourceId)
    {
        string accessToken = null;
        AuthenticationResult result = null;
        //result = await context.AcquireTokenAsync(resourceId, ClientID, _returnUri); // This Overload is not available
        result = await context.AcquireTokenAsync(resourceId, new ClientCredential(ClientID, SettingsHelper.AppKey));
        accessToken = result.AccessToken;
        return accessToken;
    }

    public static async Task<OutlookServicesClient> EnsureClientCreatedAsync()
    {
        try
        {
            string authority = CommonAuthority;
            _authenticationContext = new AuthenticationContext(authority);
            DiscoveryClient discoveryClient = new DiscoveryClient(async () => await GetTokenHelperAsync(_authenticationContext, DiscoveryResourceId));
            CapabilityDiscoveryResult result = await discoveryClient.DiscoverCapabilityAsync("Calendar");
            var client = new OutlookServicesClient(result.ServiceEndpointUri, async () => await GetTokenHelperAsync(_authenticationContext, result.ServiceResourceId));
            return client;
        }
        catch (Exception)
        {
            if (_authenticationContext != null && _authenticationContext.TokenCache != null)
                _authenticationContext.TokenCache.Clear();
            return null;
        }
    }

用于创建客户端对象

async void CreateClient()
    {
        OutlookServicesClient client = await NewOffice365Authentication.EnsureClientCreatedAsync();
        var eventsResults = await client.Me.GetCalendarView(DateTimeOffset.Now.Subtract(new TimeSpan(10, 0, 0)), DateTimeOffset.Now.AddHours(10)).ExecuteAsync();
    }

但是每次发现客户端创建时都会抛出以下错误

But each and every time the discovery client creation is throwing the following error

{"Exception of type 'Microsoft.Office365.Discovery.DiscoveryFailedException' was thrown."}
ErrorCode : Unauthorized

请帮助我创建发现客户端对象.

Please help me to create Discovery Client Object.

推荐答案

从错误中我推测您的应用程序未在Azure AD中注册,或者您的客户端ID值无效.

From the error I'm guessing that your application is not registered in Azure AD, or you have an invalid client ID value.

这篇关于无法创建用于访问Office365日历的Discovery Client对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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