Created
December 29, 2025 06:24
-
-
Save m-bartlett/8e56bb112fbf6ee9a5c68345db4e7f85 to your computer and use it in GitHub Desktop.
Direct download a file from OneDrive from a share link using shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -o errexit -o nounset | |
| error() { | |
| echo "$1" >&2 | |
| exit 1 | |
| } | |
| read-json() { | |
| local -A "j=($(sed -E 's/[{}]//g; s/"([^"]+)":"([^"]+)"/\[\1\]="\2"/g; s/,/ /g; s/\$/$$/g'))" | |
| local keys="${@:?}" key source dest values='' | |
| for key in ${keys}; do | |
| IFS=: read source dest <<<"$key" | |
| [ -z "$dest" ] && dest=$source | |
| declare -g "$dest=${j[$source]}" | |
| done | |
| } | |
| ONE_DRIVE_API_ENDPOINT="https://my.microsoftpersonalcontent.com/_api/v2.0" | |
| share_url="${1:?}" | |
| share_url_encoded="$(<<<"$share_url" base64 -w0 | tr -d '=' | tr '/+' '_-')" | |
| drive_url="$ONE_DRIVE_API_ENDPOINT/shares/u!$share_url_encoded/driveitem" | |
| read-json token:badger_token < <(curl -s \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"appId":"5cbed6ac-a083-4e14-b191-b4ba07653de2"}' \ | |
| https://api-badgerp.svc.ms/v1.0/token) | |
| [ -z "$badger_token" ] && error "Failed to get badger token" | |
| read-json @content.downloadUrl:download_url name:file_name \ | |
| < <(curl -s \ | |
| -H "Accept: application/json" \ | |
| -H "Prefer: autoredeem" \ | |
| -H "Authorization: Badger $badger_token" \ | |
| "$drive_url?select=@content.downloadUrl,name") | |
| [ -z "$download_url" ] && error "Failed to get download URL" | |
| echo "Downloading $file_name" | |
| curl --progress-bar -o "$file_name" -- $download_url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment