pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
753 stars 305 forks source link

Access denied when adding a file. #2955

Closed LenoirRemi closed 5 months ago

LenoirRemi commented 5 months ago

What version of PnPjs library you are using

3.x

Minor Version Number

3.22.0

Target environment

NodeJS

Additional environment details

I'm using NodeJS and certificate based MSAL for authentication. I'm targeting a online SharePoint instance.

Question/Request

Hello,

I'm fairly new to SharePoint development and I'm facing an issue using this library. I can do a bunch of actions on my SharePoint site but I can't upload a file. (a aspx page.)

This is how I authenticate using MSAL:

const privateKey = fs.readFileSync(process.env.PRIVATEKEY_PATH);
const configuration = {
    auth: {
        authority: `https://login.microsoftonline.com/${process.env.TENANT_ID}`,
        clientCertificate: {
            thumbprint: process.env.THUMBPRINT,
            privateKey: privateKey,
        },
        clientId: process.env.CLIENT_ID,
    }
};
const sp = spfi(`https://${process.env.TENANT_NAME}.sharepoint.com/`).using(SPDefault({
    msal: {
        config: configuration,
        scopes: [`https://${process.env.TENANT_NAME}.sharepoint.com/.default`],
    },
}));

When i try to add a page like so:

await sp.web.getFolderByServerRelativePath(folderPath).files.addUsingPath(targetPath, data, { Overwrite: true });

I get this error: HttpRequestError: Error making HttpClient request in queryable [403] Forbidden ::> {"odata.error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied."}}} There is also this inside the response: '917656; Access+denied.+Before+opening+files+in+this+location%2c+you+must+first+browse+to+the+web+site+and+select+the+option+to+login+automatically.'

I have enable all the permissions for my application on Entra ID (Like Sites.FullControl.All.)

I'm pretty sure that I missed something, but after days of trying, I feel powerless. That's why I'm creating this issue.

Looking forward to hearing your answers.

bcameron1231 commented 5 months ago

Hi, sorry you're having a tough time with this. Can you confirm that your permissions for Sites.FullControl.All have been granted by Tenant Admin for the Organization in Entra ID?

LenoirRemi commented 5 months ago

Hi, sorry you're having a tough time with this. Can you confirm that your permissions for Sites.FullControl.All have been granted by Tenant Admin for the Organization in Entra ID?

Hi, yes I think so, this is what I can see:

permissions

LenoirRemi commented 5 months ago

Hi all,

Well it looks like time was the issue there... I did not touch anything in both the code, SharePoint and Azure but somehow it starts working well suddenly.

A bit of context if others are facing this issue:

Hope it helps! 😄

github-actions[bot] commented 5 months ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.