Getting token in Office 365 Oauth2 authentication
Asked Answered
E

2

6

I am implementing Oauth 2 authentication for Office 365 account in a java based server side application. After reading the documentation, I have done the following things:

  • I have office 365 subscription.
  • I have created an app in Azure Actve directory, that is necessary to authenticate web app using office 365 account.
  • I have client ID and secret. I have also given all permission to the Azure app.
  • I am requesting authorization code using the Url:

https://login.microsoftonline.com/common/oauth2/authorize?client_id={client_id}&response_type=code&redirect_uri={redirect url}&response_mode=query

As a response of this, I am getting authorization code as expected:

http://localhost:8080?code={authorication code}&session_state=259479e4-84aa-42ea-91e9-9e919cc99587

Now I need to get token along with the user name (user ID from which the user in logged in), as I need the user name for further processing. For this, I am using the method described here:

https://azure.microsoft.com/en-us/documentation/articles/active-directory-protocols-oauth-code/

That is using a POST request like this:

POST /{tenant}/oauth2/token HTTP/1.1
Host: https://login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&client_id=2d4d11a2-f814-46a7-890a-274a72a7309e
&code=AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCmLdgfSTLEMPGYuNHSUYBrqqf_ZT_p5uEAEJJ_nZ3UmphWygRNy2C3jJ239gV_DBnZ2syeg95Ki-374WHUP-i3yIhv5i-7KU2CEoPXwURQp6IVYMw-DjAOzn7C3JCu5wpngXmbZKtJdWmiBzHpcO2aICJPu1KvJrDLDP20chJBXzVYJtkfjviLNNW7l7Y3ydcHDsBRKZc3GuMQanmcghXPyoDg41g8XbwPudVh7uCmUponBQpIhbuffFP_tbV8SNzsPoFz9CLpBCZagJVXeqWoYMPe2dSsPiLO9Alf_YIe5zpi-zY4C3aLw5g9at35eZTfNd0gBRpR5ojkMIcZZ6IgAA
&redirect_uri=https%3A%2F%2Flocalhost%2Fmyapp%2F
&resource=https%3A%2F%2Fservice.contoso.com%2F
&client_secret=p@ssw0rd

Now the problem is that, when ever I send this post request, I always get error with error code some times 400 or 402 etc. I also user POST man in chrome to check the response of the call. It always return error like that:

{
  "error": "invalid_grant",
  "error_description": "AADSTS65001: The user or administrator has not consented to use the application with ID. Send an interactive authorization request for this user and resource.\r\nTrace ID: b834315e-ccb3-4533-b7c9-4af7b34054b9\r\nCorrelation ID: 784f18da-5479-4b69-b939-0067abfcc460\r\nTimestamp: 2016-08-02 07:28:22Z",
  "error_codes": [
    65001
  ],
  "timestamp": "2016-08-02 07:28:22Z",
  "trace_id": "b834315e-ccb3-4533-b7c9-4af7b34054b9",
  "correlation_id": "784f18da-5479-4b69-b939-0067abfcc460"
}

(Note: I have registered all the apps using administrator login) I have tried a lot to find out whats is going wrong here. I have added multiple apps in Azure Active directory but I am always getting similar response.

I request professionals to help me. . . ! Actually I want to allow user to click on a button in office 365 web add-in and use oauth2 authentication to login to our system. It will be great in somebody suggest me some good tutorial to successfully implement this.

Elwoodelwyn answered 2/8, 2016 at 8:3 Comment(0)
I
0

If you were requesting the access token for the Office 365, the resource parameter in the send request should be https%3A%2F%2Foutlook.office.com.

And also you can use the Microsoft Graph as Philip suggested. You can refer here about how to choose the endpoint.

Ioyal answered 3/8, 2016 at 2:45 Comment(0)
S
0

Receiving the error codes "interaction_required" or "invalid_grant" error codes means there is an issue with the "Refresh token" and it advised to:

  • Discard current refresh token
  • Request new authorization code
Signification answered 3/7, 2018 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.