Extract text from Google Drive

A file that needs a header on every download, and an export step for anything native. Here's how a document actually gets from Google Drive to a txtfetch response.

The problem

Google Drive checks an Authorization header on every download, whether the file is a PDF you uploaded or a Doc you typed. txtfetch's ?url= fetch sends no custom headers, so it can never call files.get on your behalf. Fetch the bytes yourself first, then send them to txtfetch.

POST the bytes, and why

How Google Drive fits the decision table, cited and dated
Pre-authenticated URL?No — Drive requires a Bearer header on every download
txtfetch pathPOST the bytes
Link lifetimeN/A — no public link ever exists
The trapA native Google Doc, Sheet, or Slide has no raw bytes. Export it first, and the export tops out at 10 MB.

files.get needs an Authorization header on every download. Drive publishes no public download URL, so ?url= has nothing to reach.

How it works

Call files.get with alt=media and your own Bearer token to download an uploaded file's raw bytes. A native Google Doc, Sheet, or Slide isn't a file in that sense. Call files.export instead, and pick a format txtfetch already parses, like DOCX, XLSX, PPTX, or PDF. Export output stops at 10 MB, so a very long native Doc may need trimming first. Either way, POST the resulting bytes to txtfetch as a normal file upload.

the script

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

Python
import os

import requests

FILE_ID = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"
headers = {"Authorization": f"Bearer {os.environ['DRIVE_ACCESS_TOKEN']}"}

# A native Google Doc has no raw bytes of its own — export it first.
export = requests.get(
    f"https://www.googleapis.com/drive/v3/files/{FILE_ID}/export",
    headers=headers,
    params={"mimeType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
)
export.raise_for_status()

r = requests.post(
    "https://api.txtfetch.com/v1/extract",
    headers={"Authorization": f"Bearer {os.environ['TXTFETCH_KEY']}"},
    files={"file": ("doc.docx", export.content)},
)

print(r.json()["extracted_text"])
JavaScript
const fileId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
const headers = { Authorization: `Bearer ${process.env.DRIVE_ACCESS_TOKEN}` };

// A native Google Doc has no raw bytes of its own — export it first.
const exportUrl = new URL(`https://www.googleapis.com/drive/v3/files/${fileId}/export`);
exportUrl.searchParams.set("mimeType", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

const exported = await fetch(exportUrl, { headers });
const bytes = await exported.arrayBuffer();

const form = new FormData();
form.append("file", new Blob([bytes]), "doc.docx");

const res = await fetch("https://api.txtfetch.com/v1/extract", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.TXTFETCH_KEY}` },
  body: form,
});

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

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

frequently asked questions

Does txtfetch have a Google Drive connector?
No. txtfetch ships no Drive connector or plugin. Fetch the file yourself with the Drive API, then POST the bytes to txtfetch, the same as any other upload.
Can I just paste a Drive share link into ?url=?
No. A share link opens a Google sign-in page in a browser. txtfetch's server-side fetch has no browser session and no way to send the Authorization header Drive requires.
What if the file is a native Google Doc, not an upload?
Call files.export first, in a format txtfetch parses, like DOCX or PDF. files.get only works on files with real bytes behind them, and a native Doc has none.

Related

Point it at your Google Drive files.

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

Get an API key →

See the URL docs →