Reputation: 14949
On the CI server, I want to fetch a config file that we maintain on Github so it can be shared between many jobs. I'm trying to get this file via curl, but these approaches both fail (I get a 404):
# As advised by the oAuth docs
curl -H 'Authorization: token the_token' -L -o setup.sh https://raw.github.com/org/repo/file
# The url of the raw file after clicking to view it
curl -L https://raw.github.com/org/repo/file?login=username&token=the_token
Upvotes: 138
Views: 194234
Reputation: 8008
Both methods can achieve the goal:
Click Fine-grained tokens and then select Generate new token
.
Afterward, fill in the following:
In this case, repositories select test-data.
Then, under Repository permissions
, select Contents as Read-only.
It will also automatically set
Metadata
to Read-only.
After clicking Generate token
, the generated token will have sufficient permissions to download the files.
bash
USERNAME=""
REPOSITORY=""
TOKEN=""
OUTPUT_DIR=""
BRANCH_OR_SHA=""
FILE_PATHS=""
# example
# USERNAME="CarsonSlovoka"
# REPOSITORY="test-data"
# TOKEN="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"
# OUTPUT_DIR="$HOME/Downloads"
# BRANCH_OR_SHA="master"
# FILE_PATHS="img/pdf2/3_10.png img/pdf2/ooo.png img/pdf2/48_144.png"
for FILE in $FILE_PATHS; do
echo "download $FILE..."
output_file_path="$OUTPUT_DIR/$(basename "$FILE")"
echo "$output_file_path"
curl -H "Authorization: token $TOKEN" \
-H "Accept: application/vnd.github.v3.raw" \
-o "$output_file_path" \
"https://raw.githubusercontent.com/$USERNAME/$REPOSITORY/$BRANCH_OR_SHA/$FILE"
done
Visit this link: https://github.com/settings/tokens to new token
[x] repo Full control of private repositories
Once completed, you will get a result similar to the following. Record this token,
for example: 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
(this token is no longer valid).
Run curl
curl -H "Authorization: token " -H "Accept: application/vnd.github.v3.raw" -o OUTPUT_FILE_NAME https://raw.githubusercontent.com/USERNAME/REPO_NAME/BRACN_OR_SHA1/FILE_PATH
# example
curl -H "Authorization: token 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" -H "Accept: application/vnd.github.v3.raw" -o OUTPUT_FILE_NAME https://raw.githubusercontent.com/CarsonSlovoka/testRepo/master/subDir/test.png
or, you can create a bat file, and after executing it, the file will be downloaded.
set "USERNAME="
set "REPOSITORY="
set "TOKEN="
set "OUTPUT="
set "BRANCH_OR_SHA="
set "FILE_PATH="
curl -H "Authorization: token %TOKEN%" ^
-H "Accept: application/vnd.github.v3.raw" ^
-o %OUTPUT% ^
https://raw.githubusercontent.com/%USERNAME%/%REPOSITORY%/%BRANCH_OR_SHA%/%FILE_PATH%
pause
example:
set "USERNAME=CarsonSlovoka"
set "REPOSITORY=TestRepo"
set "TOKEN=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"
set "OUTPUT=my.png"
:: set "BRANCH_OR_SHA=master"
set "BRANCH_OR_SHA=ee8fdbf"
set "FILE_PATH=subDir/test.png"
curl -H "Authorization: token %TOKEN%" ^
-H "Accept: application/vnd.github.v3.raw" ^
-o %OUTPUT% ^
https://raw.githubusercontent.com/%USERNAME%/%REPOSITORY%/%BRANCH_OR_SHA%/%FILE_PATH%
pause
Upvotes: 1
Reputation: 1
You can create a GitHub Pages site specifically for a repository that you want to share, and you can use curl to interact with that GitHub Pages site.
example https://username.github.io/repo/filename.txt
Upvotes: -2
Reputation: 71
Useing curl with the following format:
curl -H "Authorization: token YOUR_TOKEN" -H "Accept: application/vnd.github.v3.raw" -o file.txt https://raw.githubusercontent.com/username/repository/file.txt
Settings > Developer settings > Generate new token (classic) Select scopes
Upvotes: 1
Reputation: 1
The previous answers don't work (or don't work anymore).
You can use the V3 API to get a raw file like this (you'll need an OAuth token):
curl -H 'Authorization: token INSERTACCESSTOKENHERE' \
-H 'Accept: application/vnd.github.v3.raw' \
-O \
-L https://api.github.com/repos/owner/repo/contents/path
All of this has to go on one line. The -O
option saves the file in the current directory. You can use -o filename
to specify a different filename.
To get the OAuth token follow the instructions here:
I've written this up as a gist as well:
EDIT: API references for the solution are as follows:
Upvotes: 182
Reputation: 71
Tokens (Classic)
Generate new token (classic)
curl "https://x-access-token:{put_your_token_here}@raw.githubusercontent.com/mvysotsky/docker-miner/main/Dockerfile"
Upvotes: 0
Reputation: 1719
gh
and curl
login to your account with gh auth login
curl https://$(gh auth token)@raw.githubusercontent.com/ORG/REPOS/BRANCH/PATHTO/FILE
Upvotes: -1
Reputation: 1361
Or, if you don't have a token:
curl --user [your_user] 'https://raw.github.com/path/to/file.config' > file.config
It will ask you for your password.
UPDATE: To those having 404s, you may have to create an SSH key for your machine. Refer to the SSH and GPG Keys in your Settings.
This works if you tap the Raw
button on the respective file (It's a button on the top right, available after you tap the file you want to DL). Copy the URL for that raw resource. If you attempt to DL a resource that is not hosted through raw.github.com
(it may have changed to raw.githubusercontent.com
), it won't work.
I successfully DL'ed a resource from a personal repository, as well as non-personal one, using macOS as of Jul 14 2023 using this method.
Upvotes: 7
Reputation: 2315
A simpler solution is to use Official GitHub CLI gh
.
gh auth login
For me, this command is not required since I'm already logged in.
gh
to download it:API_URL=https://api.github.com/repos/owner/repo/contents/path/file.ext
gh api $API_URL -H "Accept: application/vnd.github.raw" > file.ext
An real example is maybe better. Here it is to download install_linux.md from gh
cli:
API_URL=https://api.github.com/repos/cli/cli/contents/docs/install_linux.md
gh api $API_URL -H "Accept: application/vnd.github.raw" > install_linux.md
In API_URL
:
owner
is cli
repo
is cli
toopath/file.ext
) is docs/install_linux.md
Upvotes: 20
Reputation: 3241
A lot of the answers here either suggest to use curl
and supply your own Authorization: Bearer ****
header, or use the GitHub CLI (gh
) to get a pre-authenticated download URL and then using curl
with that URL.
However, gh
supports custom headers so you can accomplish this using gh
with a single request:
gh api "/repos/:owner/:repo/contents/README.md" -H "Accept: application/vnd.github.raw" > README.md
Upvotes: 1
Reputation: 8413
I've got a token from an app installation.
Previously, you could use the query ?access_token=MY_TOKEN
, but that has been deprecated and eventually removed in September 2021.
In their docs on Authenticating with GitHub Apps they're saying you can clone a repo with the access token and the username x-access-token
in the URL.
This also seems to work to download a raw file (the ghs_...
is the token):
$> curl "https://x-access-token:[email protected]/Octocat/codertocat/main/README.md"
Upvotes: 6
Reputation: 173
I tried a simple trick to open a GitHub private .iypnb file in Pycharm as well as Colab and it worked well for me.
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": []
}]
}
open notepad/text editor on os(eg. windows) copy all the text to a new notepad file .
save notepad as name.ipynb instead of name.txt and make save as file type All Files(.) instead of Text Documents (*.txt)
finally open file in your IDE or colab.
Upvotes: -2
Reputation: 151
I was able to get it to work for github enterprise, thanks for the suggestions above. Had to take all your suggestions and try and finally i was able to make it work. These are the steps i followed to get it to work.
https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
Make sure you have minimum following permissions for the token:
Use the following curl command to get the content:
curl -H "Authorization: token [yourPersonalToken]" -H "Accept: application/vnd.github.v3.raw" -o [filePath]-content.json -L https://github.[company].com/api/v3/repos/[ORG]/[REPO_NAME]/contents/[PATH_TO_FILE]/content.json?ref=[BRANCH_NAME]
Where->
[yourPersonalToken] is the token you created.
[filePath] is a path where you want to save the downloaded copy.
[company] is the name of company which hosted the github enterprise.
[ORG] is the github organization is which repo is created.
[REPO_NAME] is the name of the repository.
[PATH_TO_FILE] is the path where file is located.
[BRANCH_NAME] is the name of the branch you want to use, e.g. master, develop etc.
Example:
curl -H "Authorization: token 5a86ecda9ff927baaa66fad2af5bee8" -H "Accept: application/vnd.github.v3.raw" -o C:\Downloads\manifest.json -L https://github.example.com/api/v3/repos/cms/cms_one/contents/app/data/manifest.json?ref=master
Upvotes: 6
Reputation: 433
I think it is a little bit dangerous and not good way to issue a personal access token which can access all repositories even just for download a single file from my private repository.
I would love to recommend using url with token for single file. Don't worry. The token string will generated by github automatically. You can get this url on your source code page.
Upvotes: 1
Reputation: 546
For GitHub Enterprise and API v3, my bash solution looked like this (includes TOKEN cleanup / privacy):
TOKEN=yourTokenHere; history -d $((HISTCMD-1)) > /dev/null
curl -H "Authorization: token $TOKEN" \
-H 'Accept: application/vnd.github.v3.raw' \
-o file.ext \
-L http://github.company.com/api/v3/repos/[org]/[repo]/contents/path/file.ext?ref=[branch]
unset TOKEN
Upvotes: 2
Reputation: 2708
curl -H 'Authorization: token YOUR_TOKEN' \
-H 'Accept: application/vnd.github.v4.raw' \
-O \
-L https://api.github.com/repos/INSERT_OWNER_HERE/INSERT_REPO_HERE/contents/PATH/TO/FILE
So if the url of the raw file (when logged in) is
https://raw.githubusercontent.com/mr_coder/my_repo_name/master/my_script
Then
-L https://api.github.com/repos/INSERT_OWNER_HERE/INSERT_REPO_HERE/contents/PATH/TO/FILE
becomes
-L https://api.github.com/repos/mr_coder/my_repo_name/contents/my_script
Note: We have API v4
Upvotes: 2
Reputation: 600
Surprisingly none of the answers didn't worked for me until I found a workaround.
You can use personal access token https://github.com/settings/tokens as answered by @thomasfuchs
Note : while creating a token, you must check the admin permissions. See the related issue
https://github.com/octokit/octokit.net/issues/1812
Upvotes: 3
Reputation: 1763
file_name?token=ABAHQCAT6KG...
select copy -> copy link address
url is in format:
https://raw.githubusercontent.com/<USERNAME>/<PATH>/<FILENAME>?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I
in terminal:
wget -O myFilename https://raw.githubusercontent.com/<USERNAME>/<PATH>/<FILENAME>?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I
Link is valid only for limited time or you can create your token: GitHub article
Upvotes: 3
Reputation: 4479
Just an addition to the accepted answer, If you are using Github Enterprise url is slightly different:
curl -H 'Authorization: token [your token]' \
-H 'Accept: application/vnd.github.v3.raw' \
-L https://[your domain]/api/v3/repos/[owner]/[repo-name]/contents/[path of file]
Upvotes: 1
Reputation: 371
I know this is an old question, but none of the solutions proposed above worked for me. Perhaps the API has changed since then.
This worked:
curl -H 'Authorization: token [insert your token here]' -o output.txt https://raw.githubusercontent.com/[organization]/[repo]/[branch]/[path to file]
Upvotes: 25
Reputation: 87
Below should work fine. A "raw" before your branch name (master in this case).
curl -L -O https://github.com/your/repo/raw/master/fetch_file.sh
Upvotes: -4
Reputation: 1837
Alternatively, you can use a github "personal access token" (https://github.com/settings/tokens):
TOKEN=...
curl -s https://[email protected]/<user or organization>/<repo name>/<branch>/<path to file>/<file_name>
Example:
$ curl -s https://[email protected]/concourse/concourse/master/README.md
....
Upvotes: 56
Reputation: 9361
We had to download files from private GitHub repos fairly often and hacky shell scripts weren't quite cutting it, so we created fetch, which is an open source, cross-platform tool that makes it easy to download source files and release assets from a git tag, commit, or branch of public and private GitHub repos.
For example, to download the file baz
from version 0.1.3
of a private GitHub repo to /tmp
, you would do the following:
GITHUB_OAUTH_TOKEN="your token"
fetch --repo="https://github.com/foo/bar" --tag="0.1.3" --source-path="/baz" /tmp
Upvotes: 1
Reputation: 1
You can do this with a raw link.
curl -O https://raw.githubusercontent.com/owner/repo/branchname/path/to/file
Upvotes: -6
Reputation: 101
I ran into an authentication error when the url was redirected to Amazon S3:
Only one auth mechanism allowed; only the
X-Amz-Algorithm
query parameter...
Changing from the Authorization: token X
header to the ?access_token=<token>
query param worked for me.
Upvotes: 2
Reputation: 105
I was struggling with this for a few minutes until I realized all that is needed is to wrap the url in quotes to escape the ampersand.
curl "https://raw.github.com/org/repo/file?login=username&token=the_token"
That worked for me in my private repo.
Upvotes: 8