I am trying to implement FilePicker v8 in my application. I am using the Javascript Basic code for developement. For authentication I am using OAuth2 flow and getting the access token. Access token is working with api such as
https://graph.microsoft.com/v1.0/me/drives
But the problem is when I use the same access token for FilePicker, it throws an error: 'PrefetchFailure (3000003, invalid_client). Detailed error from console of FilePicker is
error
:
GetDataError: Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown.
code
:
"3000003,invalid_client"
message
:
"Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown."
I am unable to figure out why picker does not work.
Code is as below:
import { useEffect } from "react";
export interface IProps {
accessToken: string;
baseUrl: string;
onFileSelect: (files: { driveId: string; itemId: string }[]) => void; // Callback for selected files
}
export const launchPicker = async (props: IProps) => {
if (!props.baseUrl || !props.accessToken) {
console.error("Missing required parameters: baseUrl or accessToken.");
return;
}
const params = {
sdk: "8.0",
messaging: {
origin: window.location.origin,
channelId: "27",
},
entry: {
sharePoint: {},
},
authentication: {},
typesAndSources: {
mode: "all",
pivots: {
oneDrive: true,
recent: true,
},
},
};
let win: Window | null = null;
let port: MessagePort | null = null;
// Define the message listener early
const messageListener = (event: MessageEvent) => {
if (!win || event.source !== win) return;
const message = event.data;
if (message.type === "error") {
console.error("Picker Error:", message.data);
}
if (message.type === "initialize" && message.channelId === params.messaging.channelId) {
port = event.ports[0];
if (port) {
port.addEventListener("message", (msg) => handleMessage(msg.data));
port.start();
port.postMessage({ type: "activate" });
}
}
};
// Add the message listener before opening the window
window.addEventListener("message", messageListener);
// Open the picker window
win = window.open("", "Picker", "width=1080,height=680");
if (!win) {
console.error("Failed to open the picker window.");
return;
}
const queryString = new URLSearchParams({
filePicker: JSON.stringify(params),
});
const url = ${props.baseUrl}/_layouts/15/FilePicker.aspx?${queryString};
const form = win.document.createElement("form");
form.setAttribute("action", url);
form.setAttribute("method", "POST");
win.document.body.append(form);
const input = win.document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "access_token");
input.setAttribute("value", props.accessToken);
form.appendChild(input);
form.submit();
const handleMessage = (message: any) => {
switch (message.type) {
case "notification":
console.log(`Notification: ${message.data}`);
break;
case "command":
if (port) {
port.postMessage({
type: "acknowledge",
id: message.id,
});
}
const command = message.data;
switch (command.command) {
case "authenticate":
const token = props.accessToken;
if (port && token) {
port.postMessage({
type: "result",
id: message.id,
data: {
result: "token",
token,
},
});
} else {
console.error("Failed to authenticate.");
}
break;
case "close":
win?.close();
break;
case "pick":
console.log(`Picked: ${JSON.stringify(command)}`);
props.onFileSelect(command.files || []);
if (port) {
port.postMessage({
type: "result",
id: message.id,
data: { result: "success" },
});
}
win?.close();
break;
default:
console.warn(`Unsupported command: ${command.command}`);
if (port) {
port.postMessage({
result: "error",
error: {
code: "unsupportedCommand",
message: command.command,
},
isExpected: true,
});
}
break;
}
break;
default:
console.warn(`Unknown message type: ${message.type}`);
break;
}
};
// Use interval to clean up when the window is closed
const interval = setInterval(() => {
if (win?.closed) {
clearInterval(interval);
window.removeEventListener("message", messageListener);
}
}, 500);
};
Permissions available with the access token are:
SharePoint:
- AllSites.Read
- MyFiles.Read
Microsoft Graph
- Files.Read
- Files.Read.All
- Files.ReadWrite
- Files.ReadWrite.All
- Sites.Read.All
- Sites.ReadWrite.All
- User.Read