I am trying to connect to SharePoint Online with ADAL from a WCF Service that is hosted in Azure as App Service.

I configured the App, got a client ID and Secret, enables AAD auth. This works, if i browse the site in a webbrowser. I can login, get redirected to my Site where the WCF is hosted.

The WCF tries to connect to my SPO site with ADAL. I get the Access Token and add it to the client context like this

ctx_.ExecutingWebRequest += (sender, args) =>
                 AuthenticationResult token = ServiceHelper.GetAccessToken(ClientID, AppKey, TenantIDTitle, TentantID);

                 args.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + token.AccessToken;

There is a a token that I get, so no errors or anything. when I try to load the web and the execute the Context, i get an 401 unauthorized, but I don't get why. Why would I get a token if I am not allowed to access SPO with it.

Am I getting the Token wrong?

var authContext = new AuthenticationContext(AUTHORITY);
var credential = new ClientCredential(CLIENT_ID, CLIENT_SECRET);
var result = (AuthenticationResult)authContext.AcquireTokenAsync(API_ID_URL, credential).Result;
var token = result;
return token;
  • What value does API_ID_URL contain?
    – Tanmay
    Sep 22, 2016 at 10:46
  • Hi Tanmay. The API_ID_URL is: companyname.sharepoint.com/sites/mysitecollection. It is the same value as the one I entered in the manage.windowsazure.com App setting in the APP_ID_URI field
    – AdelPoint
    Sep 22, 2016 at 11:31
  • Your APP_ID_URL is probably correct but note that it should be the URL of the resource you are requesting (In your case SharePoint). You have APP_ID and resource ID as same, may be try changing your APP_ID to something else (can be anything e.g https://your-azure-tenant.com/this-app-name) and pass the SP URL in your AccquireTokenAsync Your authority should be of the form login.microsoftonline.com/tenantID client and client secret should be from the Azure AD app you registered.
    – Tanmay
    Sep 23, 2016 at 4:59

1 Answer 1


The problem was that I was doing the whole thing the wrong way: I changed it to first GetAuthorizationRequestUrlAsync to login and then AcquireTokenByAuthorizationCodeAsync with the code I get from the first method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.