Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,264 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Trying to upload files to sharepoint using graph api but getting typeerror fetch failed.It is working fine in local machine but getting above error when moved to gcp virtual machine may be because proxy issue.
// Authenticate using Client Secret credentials
const credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Initialize the Microsoft Graph client
const client = Client.initWithMiddleware({
authProvider: {
getAccessToken: async () => {
const token = await credentials.getToken("https://graph.microsoft.com/.default");
acc_tkn=token;
console.log("token",token)
return token.token;
},
},
fetchOptions:{
agent:new HttpProxyAgent('http://cloudproxy.com:8080/')
}
});
// Specify the SharePoint site and file path
const siteId ='ad2********************';
const filename=file_name
const filePath =`/PMO_Portal/${p_id}/${folder_name}/${filename}`; // Replace with your desired file path
const fileContent=filedata
console.log("filedata",fileContent)
console.log("client",client)
// Upload the file
const upload=await client.api(`/sites/${siteId}/drive/root:${filePath}:/content`).put(fileContent,(error)=>{
console.log("error is as follows",error)
})
return upload;
} catch (error) {
console.log(`Error uploading file: ${error}`);
return error;
}
TypeError: fetch failed
at node:internal/deps/undici/undici:12442:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
statusCode: -1,
code: 'TypeError',
requestId: null,
date: 2024-01-01T12:14:05.978Z,
body: 'TypeError: fetch failed',
headers: undefined
}