Skip to content

Download a file using a sharing link (guest URL).

Pass a share link generated by SharePoint (--sharing-link) — it resolves the file without the recipient needing site access.

Reference

View source

import argparse
import os
import tempfile

from office365.sharepoint.client_context import ClientContext
from tests.settings import client_id, password, site_url, tenant, username


def main():
    parser = argparse.ArgumentParser(description="Download a file using a sharing link (guest URL)")
    parser.add_argument("--sharing-link", required=True, help="file sharing link URL")
    parser.add_argument("--output", default=None, help="local output path (default: temp)")
    args = parser.parse_args()

    ctx = ClientContext(site_url).with_username_and_password(
        tenant=tenant, client_id=client_id, username=username, password=password
    )
    output = args.output or os.path.join(tempfile.mkdtemp(), "download.bin")
    with open(output, "wb") as local_file:
        ctx.web.get_file_by_guest_url(args.sharing_link).download(local_file).execute_query()
    print(f"[Ok] file has been downloaded into: {output}")


if __name__ == "__main__":
    main()

← Back to Files