Make sure you are downloading from the "raw" content URL, and not the HTML github page. I believe this is the URL for the raw JSON file you want:
https://raw.githubusercontent.com/yasuhisa1984/achieve/master/vendor/bundle/gems/fog-aws-1.2.1/lib/fog/aws/iam/default_policy_versions.json
The following code works for me with that URL:
import requests
url = "https://raw.githubusercontent.com/yasuhisa1984/achieve/master/vendor/bundle/gems/fog-aws-1.2.1/lib/fog/aws/iam/default_policy_versions.json"
resp = requests.get(url)
data = resp.json()
Answer from totalhack on Stack OverflowAs the description (located at http://developer.github.com/v3/repos/contents/) says:
/repos/:owner/:repo/contents/:path
An ajax code will be:
$.ajax({
url: readme_uri,
dataType: 'jsonp',
success: function(results)
{
var content = results.data.content;
});
Replace the readme_uri by the proper /repos/:owner/:repo/contents/:path.
This GitHub API page provides the full reference. The API endpoint for reading a file:
https://api.github.com/repos/{username}/{repository_name}/contents/{file_path}
{
"encoding": "base64",
"size": 5362,
"name": "README.md",
"content": "encoded content ...",
"sha": "3d21ec53a331a6f037a91c368710b99387d012c1",
...
}
- Consider using a personal access token
- Rate-limits (up to 60 per-hour for anonymous, up to 5,000 per-hour for authenticated) read more
- Enable accessing files in private repos
- The file content in the response is base64 encoded string
Using curl
Reading https://github.com/airbnb/javascript/blob/master/package.json using GitHub's API via curl:
curl -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/airbnb/javascript/contents/package.json
- Make sure to pass header
Accept: application/vnd.github.v3.rawto get raw file response (thanks jakub.g)
Using Python
Reading https://github.com/airbnb/javascript/blob/master/package.json using GitHub's API in Python:
import base64
import json
import requests
import os
def github_read_file(username, repository_name, file_path, github_token=None):
headers = {}
if github_token:
headers['Authorization'] = f"token {github_token}"
url = f'https://api.github.com/repos/{username}/{repository_name}/contents/{file_path}'
r = requests.get(url, headers=headers)
r.raise_for_status()
data = r.json()
file_content = data['content']
file_content_encoding = data.get('encoding')
if file_content_encoding == 'base64':
file_content = base64.b64decode(file_content).decode()
return file_content
def main():
github_token = os.environ['GITHUB_TOKEN']
username = 'airbnb'
repository_name = 'javascript'
file_path = 'package.json'
file_content = github_read_file(username, repository_name, file_path, github_token=github_token)
data = json.loads(file_content)
print(data['name'])
if __name__ == '__main__':
main()
- Define an environment variable
GITHUB_TOKENbefore running
You can use this code to see the content of the file:
from github import Github
github = Github('user', 'password')
user = github.get_user()
repository = user.get_repo('Demo')
file_content = repository.get_contents('sample.txt')
print(file_content.decoded_content.decode())
If you need to see more attributes like decoded_content, just type this:
print(help(file_content))
You can use the get_contents API
Here is an example:
from github import Github
github = Github('user', 'password')
user = github.get_user()
repository = user.get_repo('Demo')
file_content = repository.get_contents('sample.txt')
» pip install github-contents
Yes, it is different.
Github REST API download files has two steps.
1, The first step is get the download url.
The url format like this:
https://api.github.com/repos/<Project Name>/<Repository Name>/contents/<File Name>
The response format like this:

2, The second step is using the download url to get the file content.
The url format like this:
https://raw.githubusercontent.com/<Project Name>/<Repository Name>/main/<File Name>?token=<Random Token that related to Revision Version>
Please notice that the first step can't skip, otherwise you will be unable to get the revision token.
I can achieve your requirement using python:
import requests
#Define required information
project_name = "xxx"
repository_name = "xxx"
# repository_name = "xxx"
branch_name = "xxx"
File_name = "xxx"
PAT = "xxx"
url = "https://api.github.com/repos/"+project_name+"/"+repository_name+"/contents/"+File_name
#downoad file from github
payload = {}
headers = {
'Authorization': 'token '+PAT
}
#download file
file_content = requests.request("GET", ((requests.request("GET", url, headers=headers, data=payload)).json())['download_url'], headers=headers, data=payload)
print(file_content.text)
Successfully get the latest contents(I am also based on private repository):

If you want a raw file, you just need to set request header Accept with application/vnd.github.v3.raw. Just one step would be enough.
GitHub API: /repos/{owner}/{repo}/contents/{path}
Refer to:
Official way to access / download github files from scripts?
https://docs.github.com/en/rest/repos/contents