admin管理员组

文章数量:1318321

I'm attempting to connect to SharePoint using PnP JS in a Node.js server with username and password authentication, but I am encountering the following error:

I have confirmed that the environment variables (SITE_URL, USERNAME, PASSWORD) are set correctly by logging them to the console. I am using the UserCredentialsFetchClient for authentication, but I have also tried the AdalFetchClient and SPFetchClient with no success.

Things I've tried:

  1. Verified that the environment variables are correctly loaded.

  2. Ensured the URL format (process.env.SITE_URL) is correct.

  3. Tried different authentication clients (AdalFetchClient, SPFetchClient).

  4. Checked if the SharePoint site is accessible with the provided credentials.

What am I missing or doing wrong?
Any help or suggestions on how to resolve the URL parsing issue or how to correctly authenticate using username and password with PnP JS in Node.js would be greatly appreciated.

import { sp } from '@pnp/sp-commonjs';
import nodejsCommonjs from "@pnp/nodejs-commonjs";
import dotenv from 'dotenv';

dotenv.config();

const { UserCredentialsFetchClient } = nodejsCommonjs;

// Setup PnP JS with username/password authentication
sp.setup({
    sp: {
        fetchClientFactory: () => {
            return new UserCredentialsFetchClient(
                process.env.SITE_URL,
                process.env.USERNAME,
                process.env.PASSWORD
            );
        },
    },
});

// Function to check PnP connection
export const checkConnection = async () => {
    try {
        const web = await sp.web.select('Title')();
        console.log("web...", web);
        console.log(`Connected to SharePoint site: ${web.Title}`);
        return { success: true, siteTitle: web.Title };
    } catch (error) {
        console.error('Error connecting to SharePoint:', error.message);
        return { success: false, error: error.message };
    }
};

// Test the connection
checkConnection().then((result) => {
    if (result.success) {
        console.log('PnP connection successful:', result.siteTitle);
    } else {
        console.log('PnP connection failed:', result.error);
    }
});

本文标签: