Extract text from SharePoint & OneDrive

The download URL that needs no Authorization header at all. Here's how a document actually gets from SharePoint & OneDrive to a txtfetch response.

The problem

SharePoint and OneDrive both sit behind Microsoft Graph. Graph already solved the header problem: its download URL needs no Authorization header of its own. The catch is how briefly that URL stays valid.

?url=, and why

How SharePoint & OneDrive fits the decision table, cited and dated
Pre-authenticated URL?Yes — @microsoft.graph.downloadUrl needs no header
txtfetch path?url=
Link lifetimeMinutes — Microsoft warns it can expire fast
The trapThe URL might expire within minutes. Fetch it right before the txtfetch call, never from a queue or a retry.

Microsoft Graph's @microsoft.graph.downloadUrl is pre-authenticated. It carries its own short-lived access, so ?url= reaches it with no header.

How it works

Request a driveItem with $select=@microsoft.graph.downloadUrl, and Graph returns that URL directly in the JSON body. Pass it straight to txtfetch's ?url= parameter. GET .../content works too — Graph replies with a 302 redirect to the same URL, and txtfetch follows redirects automatically. Use the JSON form for a browser-side call, since a browser blocks that redirect with a CORS preflight. A server-side call can use either form.

the script

A plain HTTP call against SharePoint & OneDrive's own REST API, then a plain call to txtfetch. No vendor SDK either side.

Python
import os

import requests

ITEM_ID = "01BYE5RZ6QN3ZWBTUFOFD3GSPGOHDJD4NX"
headers = {"Authorization": f"Bearer {os.environ['GRAPH_ACCESS_TOKEN']}"}

item = requests.get(
    f"https://graph.microsoft.com/v1.0/me/drive/items/{ITEM_ID}",
    headers=headers,
    params={"select": "@microsoft.graph.downloadUrl"},
)
item.raise_for_status()
download_url = item.json()["@microsoft.graph.downloadUrl"]

r = requests.post(
    "https://api.txtfetch.com/v1/extract",
    headers={"Authorization": f"Bearer {os.environ['TXTFETCH_KEY']}"},
    params={"url": download_url},
)

print(r.json()["extracted_text"])
JavaScript
const itemId = "01BYE5RZ6QN3ZWBTUFOFD3GSPGOHDJD4NX";

const graphRes = await fetch(
  `https://graph.microsoft.com/v1.0/me/drive/items/${itemId}?select=@microsoft.graph.downloadUrl`,
  { headers: { Authorization: `Bearer ${process.env.GRAPH_ACCESS_TOKEN}` } },
);
const { "@microsoft.graph.downloadUrl": downloadUrl } = await graphRes.json();

const endpoint = new URL("https://api.txtfetch.com/v1/extract");
endpoint.searchParams.set("url", downloadUrl);

const res = await fetch(endpoint, {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.TXTFETCH_KEY}` },
});

const { extracted_text } = await res.json();
console.log(extracted_text);

txtfetch ships no connector, plugin, or client for SharePoint & OneDrive. The script above is the whole integration. Fetch the document with SharePoint & OneDrive's own API, then hand it to txtfetch, the same as any other source of text.

frequently asked questions

Does txtfetch have a SharePoint or OneDrive connector?
No. txtfetch ships no connector for either. The Graph download URL already needs no header, so ?url= reaches it directly with no extra plumbing.
Why does the download URL expire so fast?
Microsoft designs it that way on purpose. It's meant for an immediate download, not for storing and reusing later, so fetch it right before you call txtfetch.
Can I call the download URL from a browser instead of a server?
Not through GET .../content — a browser blocks its 302 redirect with a CORS preflight. Request @microsoft.graph.downloadUrl directly and fetch that URL instead.

Related

Point it at your SharePoint & OneDrive files.

Pass a signed URL and the text comes back. There is no connector to install.

Get an API key →

See the URL docs →