Microsoft Graph API error: Access token validation failure. Invalid audience.

MOUSSAOUI Mohammed 40 Reputation points
2024-08-20T13:48:26.91+00:00

Hello,

I am encountering a small problem with the API key. I would like to fetch data from a file located in a SharePoint library using JavaScript.
User's image

I created an application on SharePoint and granted it the following permissions:

**
User's image

I used Postman to generate an access token, but when I try to use this access token, it doesn't work either on Postman or in JavaScript.

User's image

I successfully generated an access token, but when I try to use it, I encounter an error.

  1. Error on Postman:

User's image

  1. Error on JS (Here is my JavaScript code:

User's image

User's image

Could you please help me resolve this issue? Thank you.

Microsoft 365 and Office | SharePoint | Development
Microsoft Security | Microsoft Graph
{count} votes

Answer accepted by question author
  1. CarlZhao-MSFT 46,406 Reputation points
    2024-08-21T02:37:18.9166667+00:00

    Hi @MOUSSAOUI Mohammed

    The 00000002-0000-0000-c000-000000000000 is the application ID for Azure AD Graph API, which has been deprecated and cannot be used to call MS Graph API. You should request a token for MS Graph API, please follow this documentation.

    Additionally, if the audience of the token you request in JS is MS Graph API, do not apply it to SharePoint REST API, otherwise it will still throw a 401 error.

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    1 person found this answer helpful.

6 additional answers

Sort by: Most helpful
  1. MOUSSAOUI Mohammed 40 Reputation points
    2024-08-21T09:15:18.6266667+00:00

    ................


  2. MOUSSAOUI Mohammed 40 Reputation points
    2024-08-21T15:00:19.0466667+00:00

    Can you tell me how to do this please, it's the first time I've used this.

    $(document).ready(function() {
      fetchCSV();
    });
    
    async function fetchCSV() {
      const siteUrl = "https://awex.sharepoint.com";
      const fileUrl = "/sites/Intranet/Documents%20partages/AdUserlast180days.csv";
      const accessToken = ''; 
      
      try {
        const response = await fetch(`${siteUrl}/_api/web/GetFileByServerRelativeUrl('${fileUrl}')/$value`, {
      method: 'GET',
      headers: {
        'Authorization': 'Bearer ' + accessToken,
        'Accept': 'text/csv',
      },
      mode: 'no-cors' 
    });
    
        if (!response.ok) {
          throw new Error(`Erreur HTTP ! Statut : ${response.status}`);
        }
    
        const csvData = await response.text();
        console.log(csvData); 
        console.log('Fichier chargé avec succès');
    
        // Traitez le CSV si nécessaire
        // Par exemple, utilisez PapaParse pour l'analyser
        // const parsedData = Papa.parse(csvData, { header: true });
        // console.log(parsedData);
    
      } catch (error) {
        console.error('Erreur lors de la récupération du fichier CSV :', error);
      }
    }
    
    
    0 comments No comments

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.