How do I properly configure the OneDrive / SharePoint File Picker for use with internal Sharepoint storage?

Background

I have followed all Microsoft’s documentation, and have viewed several of their samples. It appears that I’m doing everything correctly. The steps I have taken so far are as follows.

Configuration

This is a business sharepoint, so the base url is https://{tenant}-my.sharepoint.com/_layouts/15/FilePicker.aspx. I have validated that I can access the file picker by manually navigating to that control with no issues.

We get a SharePoint token by refreshing our AAD Graph token with the https://{tenant}-my.sharepoint.com/.default scope. The Graph token has the following in addition to other scopes necessary for my app:

  • User.Read
  • Files.Read
  • Files.Read.All
  • Sites.Read.All

My app is a Graph app, but just in case, I also added the Sharepoint Delegated permissions

  • MyFiles.Read
  • AllSites.Read

The token refreshes as expected, and I receive a jwt with, among other properties:

"aud": "https://{tenant}-my.sharepoint.com",
"scp": "User.Read Files.Read Files.Read.All Sites.Read.All ..."

I then configure the picker with the following, however, it never reaches the initializeMessageListener event.

const click = (e: MouseEvent<HTMLButtonElement>) => {
  e.preventDefault();

  const win = window.open("", "Picker", "width=800,height=600");

  if (!win) {
    throw new Error("Could not open picker window");
  }

  setupPicker(win);
};

const setupPicker = async (win: Window) => {
  const options: IFilePickerOptions = {
    sdk: "8.0",
    entry: {
      sharePoint: {},
    },
    authentication: {},
    messaging: {
      origin: window.location.origin, // http://localhost:5173
      channelId: "27", // hard-coded for now, as in the samples
    },
    typesAndSources: {
      mode: "files",
    },
  };

  const queryString = new URLSearchParams({
    filePicker: JSON.stringify(options),
  });

  const accessToken = `Bearer ${await getAccessToken()}`; // access token with properties above
  const url = `${baseUrl}?${queryString}`;

  const form = win.document.createElement("form");
  form.setAttribute("action", url);
  form.setAttribute("method", "POST");
  win.document.body.appendChild(form);

  const input = win.document.createElement("input");
  input.setAttribute("type", "hidden");
  input.setAttribute("name", "access_token");
  input.setAttribute("value", accessToken);
  form.appendChild(input);

  win.addEventListener("message", initializeMessageListener);
  form.submit();
};

const initializeMessageListener = async (event: MessageEvent) => {
  console.log(event);

  // ideally I would setup the port here, but I never receive any messages from the window
};

I never receive any messages from the window. For the first 40 seconds, it displays an empty sharepoint picker. After that, it times out, and I receive a setup timeout error.

My gut tells me that maybe there’s some kind of configuration issue with messaging. I’m using window.location.origin which in this case resolves to http://localhost:5173… That’s my local app. I don’t see why I would receive no messages.

Debugging Steps

I took my sharepoint token, and attempted to call the sharepoint api at

https://{tenant}-my.sharepoint.com/_api/web/folders
  • Initially, I received a 403 Access Denied response.
  • I removed the Authorization header, and received 403 Attempted to perform an unauthorized operation suggesting that I was originally properly assigning the token to the request.
  • I tried refreshing the token with the scope https://{tenant}.sharepoint.com/.default just to see what would happen. It gave me 401 unauthorized with the exception Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException, suggesting that my original token did have the correct audience.

I am able to call

https://{tenant}-my.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/my')/Files

However, it returns an empty object (which is not accurate)

{
    "d": {
        "results": []
    }
}

If I try to access the folder directly, I get 403 Access Denied

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật