one drive shared links API

Igor Babushkin 0 Reputation points
2024-11-16T12:10:51.3033333+00:00
import * as msal from '@azure/msal-node';
const msalClient = new msal.ConfidentialClientApplication({
      auth: {
        clientId: CONFIG.ONE_DRIVE_CLIENT_ID,
        authority: `https://login.microsoftonline.com/${CONFIG.ONE_DRIVE_TENANT_ID}`,
        clientSecret: CONFIG.ONE_DRIVE_CLIENT_SECRET,
      },
})

const accessData = await msalClient.acquireTokenByClientCredential({
      scopes: ['https://graph.microsoft.com/.default'],
});


const sharedLink =
      'https://1drv.ms/f/s!PIBLIC_SHARED_LINK_DATA?e=
PIBLIC_SHARE_LINK_DATA
'; // has access to edit for everyone with a link
const base64Value = Buffer.from(sharedLink, 'utf-8').toString('base64');
const encodedUrl =
      'u!' +
      base64Value.replace(/=+$/, '').replace(/\//g, '_').replace(/\+/g, '-');

const response = await fetch(
      `https://graph.microsoft.com/v1.0/shares/${encodedUrl}/`,
      {
        headers: {
          Authorization: `${accessData.tokenType} ${accessData.accessToken}`,
        },
      },
);
if (response.ok) {
      const data = await response.json();
      console.log('Shared folder items:', data);
} else {
      console.error(
        'Error retrieving shared items:',
        response.status,
        response.statusText,
      );
}

I get access token but GET /shares response error 403

I have created an application for my OneDrive business account, and register it by Microsoft Entra and Azure. I tried to activate trial for my directory.
I already granted all the necessary permisions and allow external sharing here: Microsoft 365 Admin Center > Settings > Org Settings > SharePoint
And it still doesn't work, even through https://developer.microsoft.com/en-us/graph/graph-explorer

User's image

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,506 questions
OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
1,189 questions
Microsoft Entra
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yakun Huang-MSFT 7,430 Reputation points Microsoft Vendor
    2024-11-18T06:59:55.6566667+00:00

    Hello Igor Babushkin,

    Thank you for reaching out to Microsoft Support!

    When tested, the endpoint worked fine, and during the test, a 403 error occurred when the Application permissions were not present in the token Files.ReadWrite.All.

    Parse your token in JWT.ms to check that you have valid permissions in the roles parameter.

    Screenshot 2024-11-18 065846

    Hope this helps.

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


Your answer

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