BaBu
BaBu

Reputation: 1943

Office365 REST API. Getting a 401 (Unauthorized) using ADAL

I'm trying to access the Office365 REST API using the ADAL library but get a 401 (Unauthorized), and can't figure out why. I have registered an application in our Azure AD, got an id, fetched a key for it and given it permissions to read the Windows Azure Active Directory.

I am able to acquire an OAuth token but it won't play with the Office365 API.

Here is one of my unsuccessful attempts:

  AuthenticationContext auth = new AuthenticationContext("https://login.windows.net/" + myTenant);
  AuthenticationResult authenticationResult = auth.AcquireToken("http://Rest", new ClientCredential(myId, myKey));
  HttpClient client = new HttpClient();
  client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
  HttpResponseMessage msg = await client.GetAsync("https://outlook.office365.com/api/v1.0/me/events");
  // => Bam. 401. :(

Any ideas?

Upvotes: 1

Views: 3318

Answers (1)

Jason Johnston
Jason Johnston

Reputation: 17702

The resource ID you're passing to AcquireToken is invalid. That should be "https://outlook.office365.com/". See if that helps. If not, try parsing your token and look for issues: https://github.com/jasonjoh/office365-azure-guides/blob/master/ValidatingYourToken.md

Upvotes: 2

Related Questions