title: Getting token in Office 365 Oauth2 authentication
body: <p>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:</p>
  
  <ul>
  <li>I have office 365 subscription. </li>
  <li>I have created an app in Azure
     Actve directory, that is necessary to authenticate web app using
     office 365 account. </li>
  <li>I have client ID and secret. I have also
     given all permission to the Azure app. </li>
  <li>I am requesting authorization code using the Url:</li>
  </ul>
  
  <blockquote>
    <p><a href="https://login.microsoftonline.com/common/oauth2/authorize?client_id=" rel="nofollow">https://login.microsoftonline.com/common/oauth2/authorize?client_id=</a>{client_id}&amp;response_type=code&amp;redirect_uri={redirect url}&amp;response_mode=query</p>
  </blockquote>
  
  <p>As a response of this, I am getting authorization code as expected:</p>
  
  <pre><code>http://localhost:8080?code={authorication code}&amp;session_state=259479e4-84aa-42ea-91e9-9e919cc99587
  </code></pre>
  
  <p>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:</p>
  
  <blockquote>
    <p><a href="https://azure.microsoft.com/en-us/documentation/articles/active-directory-protocols-oauth-code/" rel="nofollow">https://azure.microsoft.com/en-us/documentation/articles/active-directory-protocols-oauth-code/</a> </p>
  </blockquote>
  
  <p>That is using a POST request like this:</p>
  
  <pre><code>POST /{tenant}/oauth2/token HTTP/1.1
  Host: https://login.microsoftonline.com
  Content-Type: application/x-www-form-urlencoded
  grant_type=authorization_code
  &amp;client_id=2d4d11a2-f814-46a7-890a-274a72a7309e
  &amp;code=AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCmLdgfSTLEMPGYuNHSUYBrqqf_ZT_p5uEAEJJ_nZ3UmphWygRNy2C3jJ239gV_DBnZ2syeg95Ki-374WHUP-i3yIhv5i-7KU2CEoPXwURQp6IVYMw-DjAOzn7C3JCu5wpngXmbZKtJdWmiBzHpcO2aICJPu1KvJrDLDP20chJBXzVYJtkfjviLNNW7l7Y3ydcHDsBRKZc3GuMQanmcghXPyoDg41g8XbwPudVh7uCmUponBQpIhbuffFP_tbV8SNzsPoFz9CLpBCZagJVXeqWoYMPe2dSsPiLO9Alf_YIe5zpi-zY4C3aLw5g9at35eZTfNd0gBRpR5ojkMIcZZ6IgAA
  &amp;redirect_uri=https%3A%2F%2Flocalhost%2Fmyapp%2F
  &amp;resource=https%3A%2F%2Fservice.contoso.com%2F
  &amp;client_secret=p@ssw0rd
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>{
    "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"
  }
  </code></pre>
  
  <p>(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. </p>
  
  <p>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.</p>