1

I'm having a kind of weird problem. So I'm trying to connect to Office365 in a Windows Universal App. I've found Code on the Internet, that worked. I tried it in a test-Project (in Visual Studio 2015) and it worked. So I copied it into the project, that i needed it in. This is the Code:

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

While Debugging it stops at the line

WebTokenRequestResult webTokenRequestResult = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(webTokenRequest);

and doesn't continue to debug anymore.

Like I said. I have a project with exactly the same code as shown and it works perfectly fine. Any Ideas?

N. Krh
  • 61
  • 8

1 Answers1

0

I think your problem is with async debugging behaviour, not with the API in question. https://msdn.microsoft.com/en-us/library/jj155813.aspx may be of help.

Martin K
  • 183
  • 9