即使使用正确的凭据,也取消授权创建 DiscoveryClient 的问题
Unauthorize Issue creating DiscoveryClient even with correct Credentials
我的问题与此线程类似
Unable to create Discovery Client object for Accessing Office365 calendar
我 100% 确定客户端凭据是正确的,因为我正在获取访问令牌,但在 DiscoveryClient 的过程中,我收到 DiscoveryFailedException -> Uauthorized
public Uri discoverySer = new Uri("https://api.office.com/discovery/v1.0/me/");
public string discoveryId = "https://api.office.com/discovery/";
public string cmmAth = "https://login.windows.net/Common";
public static AuthenticationContext _authenticationContext { get; set; }
public string _settingCliID { get; set; }
public string _settingCliSec {get; set; }
public async void CreateClient()
{
NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("appSettings");
_settingCliID = section["ida:ClientID"];
_settingCliSec = section["ida:Password"];
OutlookServicesClient outCli = await EnsureClientCreatedAsync();
var eventResults = await outCli.Me.Events.ExecuteAsync();
}
public async Task<OutlookServicesClient> EnsureClientCreatedAsync()
{
try
{
string authority = cmmAth;
_authenticationContext = new AuthenticationContext(authority);
DiscoveryClient discoCli = new DiscoveryClient(discoverySer ,async () => await GetTokenHelperAsync(_authenticationContext, discoveryId));
CapabilityDiscoveryResult discoResult = await discoCli.DiscoverCapabilityAsync("Calendar");
var outlookCli = new OutlookServicesClient(discoResult.ServiceEndpointUri, async () => await GetTokenHelperAsync(_authenticationContext, discoResult.ServiceResourceId));
return outlookCli;
}
catch (DiscoveryFailedException e)
{
if(_authenticationContext != null && _authenticationContext.TokenCache != null){
_authenticationContext.TokenCache.Clear();
}
MessageBox.Show(e.Source);
return null;
}
catch(AdalException e){
MessageBox.Show(e.ErrorCode);
return null;
}
}
public async Task<string> GetTokenHelperAsync(AuthenticationContext context, string resourceId)
{
string accessToken = null;
AuthenticationResult result = null;
ClientCredential cliCred = new ClientCredential(_settingCliID, _settingCliSec);
result = await context.AcquireTokenAsync(resourceId, cliCred);
accessToken = result.AccessToken;
return accessToken;
}
提前感谢您的帮助
马切
我刚刚与 Discovery API 人员确认它不支持客户端凭据,这样就可以解释了!正如我的评论中所建议的,只需跳过发现。
我的问题与此线程类似
Unable to create Discovery Client object for Accessing Office365 calendar
我 100% 确定客户端凭据是正确的,因为我正在获取访问令牌,但在 DiscoveryClient 的过程中,我收到 DiscoveryFailedException -> Uauthorized
public Uri discoverySer = new Uri("https://api.office.com/discovery/v1.0/me/");
public string discoveryId = "https://api.office.com/discovery/";
public string cmmAth = "https://login.windows.net/Common";
public static AuthenticationContext _authenticationContext { get; set; }
public string _settingCliID { get; set; }
public string _settingCliSec {get; set; }
public async void CreateClient()
{
NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("appSettings");
_settingCliID = section["ida:ClientID"];
_settingCliSec = section["ida:Password"];
OutlookServicesClient outCli = await EnsureClientCreatedAsync();
var eventResults = await outCli.Me.Events.ExecuteAsync();
}
public async Task<OutlookServicesClient> EnsureClientCreatedAsync()
{
try
{
string authority = cmmAth;
_authenticationContext = new AuthenticationContext(authority);
DiscoveryClient discoCli = new DiscoveryClient(discoverySer ,async () => await GetTokenHelperAsync(_authenticationContext, discoveryId));
CapabilityDiscoveryResult discoResult = await discoCli.DiscoverCapabilityAsync("Calendar");
var outlookCli = new OutlookServicesClient(discoResult.ServiceEndpointUri, async () => await GetTokenHelperAsync(_authenticationContext, discoResult.ServiceResourceId));
return outlookCli;
}
catch (DiscoveryFailedException e)
{
if(_authenticationContext != null && _authenticationContext.TokenCache != null){
_authenticationContext.TokenCache.Clear();
}
MessageBox.Show(e.Source);
return null;
}
catch(AdalException e){
MessageBox.Show(e.ErrorCode);
return null;
}
}
public async Task<string> GetTokenHelperAsync(AuthenticationContext context, string resourceId)
{
string accessToken = null;
AuthenticationResult result = null;
ClientCredential cliCred = new ClientCredential(_settingCliID, _settingCliSec);
result = await context.AcquireTokenAsync(resourceId, cliCred);
accessToken = result.AccessToken;
return accessToken;
}
提前感谢您的帮助
马切
我刚刚与 Discovery API 人员确认它不支持客户端凭据,这样就可以解释了!正如我的评论中所建议的,只需跳过发现。