最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Error connecting to SharePoint using PnP JS in Node.js with Username and Password Authentication: Failed to parse URL - Stack Ov

programmeradmin1浏览0评论

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);
    }
});
发布评论

评论列表(0)

  1. 暂无评论