How to fix StatusCodeError: 403 - {"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"}}}

Shubham Biswas 20 Reputation points
2025-01-22T07:26:58.0933333+00:00

this is my code

this.router.post('/listen', async (req: Request, res: Response) => {
            if (req.query.validationtoken) {
                console.log('Validation token received:', req.query.validationtoken);
                res.status(200).send(req.query.validationtoken);
            } else {
                try {
                    const data = await this.changeHelper.check(req.body);
                    console.log('Change data:', data);

                    const fileName = path.join(__dirname, '../../public/webhook.txt');
                    const fileData = fs.existsSync(fileName) ? fs.readFileSync(fileName, 'utf-8') : '';
                    const newContent = `<b>Retrieved</b>: ${moment().toISOString()}<br>${JSON.stringify(data)}<br><br>${fileData}`;

                    fs.writeFileSync(fileName, newContent, 'utf-8');
                    res.sendStatus(200);
                } catch (error) {
                    console.error('Error processing webhook notification:', error);
                    res.status(400).json({ error: 'Invalid webhook data' });
                }
            }
        });

        this.router.get('/create', async (req: Request, res: Response) => {
            try {
                console.log('Calling createSubscription method');
                const token = await this.tokenHelper.getAppOnlyAccessToken(config);
                const response = await request.post({
                    uri: `${config.webhookConfig.url}/_api/web/lists('${config.webhookConfig.listId}')/subscriptions`,
                    headers: {
                        Authorization: `Bearer ${token}`,
                        Accept: 'application/json;odata=verbose',
                        'Content-Type': 'application/json'
                    },
                    body: {
                        resource: `${config.webhookConfig.url}/_api/web/lists('${config.webhookConfig.listId}')`,
                        notificationUrl: config.adalConfig.subscriptionUrl,
                        expirationDateTime: moment().add(0.5, 'days').toISOString(),
                        clientState: config.webhookConfig.clientState
                    },
                    json: true
                });
                res.status(200).json(response);
            } catch (error) {
                console.error('Error creating subscription:', error);
                res.status(500).json({ error: 'Failed to create subscription' });
            }
        });

        return this.router;
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,173 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xyza Xue_MSFT 28,051 Reputation points Microsoft Vendor
    2025-01-22T09:09:23.0633333+00:00

    Hi @Shubham Biswas ,

    Thank you for posting in this community.

    This error typically indicates that the user or application does not have the necessary permissions to perform the requested action.

    Here are some steps you can take to resolve this issue:

    1.Check Permissions: Ensure that the user or application has the necessary permissions to access the SharePoint site and perform the requested action.

    2.Use Correct Credentials: Make sure that the client ID and secret key provided are correct and valid. You can also try regenerating the client ID and secret key and retrying the connection.


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

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.