Unable to create Discovery Client object for Accessing Office365 calendar

会有一股神秘感。 提交于 2019-12-07 11:50:49

问题


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

For creating the client object

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.


回答1:


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



来源:https://stackoverflow.com/questions/28018843/unable-to-create-discovery-client-object-for-accessing-office365-calendar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!