问题
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