I have integrated an office 365 trial domain into my Azure AD. In the Azure AD I have created a custom application and given it the client key and client secret. Now I am trying to authenticate a simple application using the client id and client secret, get the access token and make a REST call to SharePoint Online. I have given the necessary permissions to the application to make the REST calls. However when I make the call I get an unauthorized error. Does anyone know what is going wrong here. This is the code I am using.

//Code to get the access token through ADAL

    AuthenticationContext authenticationContext = new AuthenticationContext("https://login.windows.net/mytenant.onmicrosoft.com", false);
    ClientCredential clientCred = new ClientCredential(clientID, clientSecret);
    AuthenticationResult authenticationResult = authenticationContext.AcquireToken("https://mytenant.sharepoint.com", clientCred);

//Code to make the call

     string requestUrl = "https://mytenant.sharepoint.com/sites/TeamSite/_api/Web/Lists";
     HttpClient client = new HttpClient();
     HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
     request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/atom+xml"));
     request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
     HttpResponseMessage response = await client.SendAsync(request);
     if (response.IsSuccessStatusCode) //I get unauthorized here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.