我有一种奇怪的问题。所以我试图在Windows通用应用程序中连接到Office365。我在互联网上找到了Code,这很有效。我在测试项目(在Visual Studio 2015中)中尝试过它并且它有效。所以我将它复制到项目中,我需要它。 这是代码:
public async void ConnectOffice()
{
string accessToken = await GetAccessTokenForResource("https://api.office.com/discovery/");
DiscoveryClient discoveryClient = new DiscoveryClient(() =>
{
return accessToken;
});
CapabilityDiscoveryResult result = await discoveryClient.DiscoverCapabilityAsync("RootSite");
var sharePointAccessToken = await GetAccessTokenForResource(result.ServiceResourceId);
var sharePointServiceEndpointUri = result.ServiceEndpointUri.ToString();
// Change global variable
o365serverstring = result.ServiceResourceId;
}
public async Task<string> GetAccessTokenForResource(string resource)
{
string clientId = App.Current.Resources["ida:ClientId"].ToString();
string token = null;
//first try to get the token silently
WebAccountProvider aadAccountProvider = await WebAuthenticationCoreManager.FindAccountProviderAsync("https://login.windows.net");
WebTokenRequest webTokenRequest = new WebTokenRequest(aadAccountProvider, String.Empty, clientId, WebTokenRequestPromptType.Default);
webTokenRequest.Properties.Add("authority", "https://login.windows.net");
webTokenRequest.Properties.Add("resource", resource);
WebTokenRequestResult webTokenRequestResult = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(webTokenRequest);
if (webTokenRequestResult.ResponseStatus == WebTokenRequestStatus.Success)
{
WebTokenResponse webTokenResponse = webTokenRequestResult.ResponseData[0];
token = webTokenResponse.Token;
}
else if (webTokenRequestResult.ResponseStatus == WebTokenRequestStatus.UserInteractionRequired)
{
//get token through prompt
webTokenRequest = new WebTokenRequest(aadAccountProvider, String.Empty, clientId, WebTokenRequestPromptType.ForceAuthentication);
webTokenRequest.Properties.Add("authority", "https://login.windows.net");
webTokenRequest.Properties.Add("resource", resource);
webTokenRequestResult = await WebAuthenticationCoreManager.RequestTokenAsync(webTokenRequest);
if (webTokenRequestResult.ResponseStatus == WebTokenRequestStatus.Success)
{
WebTokenResponse webTokenResponse = webTokenRequestResult.ResponseData[0];
token = webTokenResponse.Token;
}
}
return token;
}
调试时停在
行WebTokenRequestResult webTokenRequestResult = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(webTokenRequest);
并且不再继续调试。
像我说的那样。我有一个项目与所示的完全相同的代码,它工作得很好。 任何想法?答案 0 :(得分:0)
我认为你的问题在于异步调试行为,而不是有问题的API。 https://msdn.microsoft.com/en-us/library/jj155813.aspx可能会有所帮助。