title: Access sharppoint online rest apis via external gmail user
body: <p>Really need your help here. struggling with this for almost a week and still have not found how to do it.</p>
  
  <p>Get access denied all the time.</p>
  
  <p>Browser scenario works for me</p>
  
  <p>I have a gmail user let's name it testuser@gmail.com which is registered in sharepoint and have permissions to access a site. </p>
  
  <p>In the browser, when I enter <a href="https://mytenant.sharepoint.com/subSite2" rel="nofollow noreferrer">https://mytenant.sharepoint.com/subSite2</a>  it uses Ouath2, it prompts me to select the user </p>
  
  <p>I select testuser@gmail.com and then it signs me in.</p>
  
  <p>Then I run the following sharepoint rest api <a href="https://mytenant.sharepoint.com/subSite2/_api/web" rel="nofollow noreferrer">https://mytenant.sharepoint.com/subSite2/_api/web</a> and it works!!!</p>
  
  <p>I have no problem with none external users, I use the <a href="https://paulryan.com.au/2014/spo-remote-authentication-rest/" rel="nofollow noreferrer">https://paulryan.com.au/2014/spo-remote-authentication-rest/</a> </p>
  
  <p>article and all works!!.</p>
  
  <p>For external user I understand that I need to use the Oauth2 authentication.</p>
  
  <p>I have an app registered in Azure AD with Sharepoint Sites.Read.All and Sites.Search.All.</p>
  
  <p>I'm using java, and I tried to do it with code with all possible oauth2 authentication flows (<a href="https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows</a>), using MSAL and ADAL.</p>
  
  <p>I pass the client id and secret and manage to login, but when I run the rest api with the required headers:</p>
  
  <pre><code>Authorization: Bearer &lt;token_access&gt;
  Accept: application/json;odata=nometadata
  </code></pre>
  
  <p>I get:
  <code>{"error_description":"Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown."}</code></p>
  
  <p>Can anyone give me a clue how to do this?? This is the list of stuff I tried:</p>
  
  <p><a href="https://www.advaiya.com/blog/access-sharepoint-rest-api-using-oauth/" rel="nofollow noreferrer">https://www.advaiya.com/blog/access-sharepoint-rest-api-using-oauth/</a></p>
  
  <p><a href="https://github.com/AzureAD/microsoft-authentication-library-for-java/wiki/Scenarios" rel="nofollow noreferrer">https://github.com/AzureAD/microsoft-authentication-library-for-java/wiki/Scenarios</a></p>
  
  <p>Download the samples and none of it work for me </p>
  
  <p><a href="https://medium.com/@anoopt/accessing-sharepoint-data-using-postman-sharepoint-rest-api-76b70630bcbf" rel="nofollow noreferrer">https://medium.com/@anoopt/accessing-sharepoint-data-using-postman-sharepoint-rest-api-76b70630bcbf</a>
  (but this does not include using a user :( )</p>
  
  <p>Thank you very much!!!</p>