我需要将Office 365日历集成到我的应用程序中。 我这样做
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();
}
但每次发现客户端创建都会抛出以下错误
{"Exception of type 'Microsoft.Office365.Discovery.DiscoveryFailedException' was thrown."}
ErrorCode : Unauthorized
请帮我创建Discovery客户端对象。
答案 0 :(得分:0)
从错误中我猜测您的应用程序未在Azure AD中注册,或者您的客户端ID值无效。