Skip to content

Instantly share code, notes, and snippets.

@CarsonSlovoka
Last active June 18, 2021 12:37
Show Gist options
  • Save CarsonSlovoka/b9be9d0001d62706b0b6eb7b865a4fa3 to your computer and use it in GitHub Desktop.
Save CarsonSlovoka/b9be9d0001d62706b0b6eb7b865a4fa3 to your computer and use it in GitHub Desktop.
access GitHub repositories by Python
import requests
token = "MY_SECRET_TOKEN"
owner = 'vinta'
repo = 'awesome-python'
path = 'docs/CNAME'
branch = 'master' # or sha1, for example: 6831740
url = f'https://api.github.com/repos/{owner}/{repo}/contents/{path}?ref={branch}'
print(url)
r = requests.get(url,
headers={
'accept': 'application/vnd.github.v3.raw',
# 'authorization': f'token {token}', # If you are want to read "public" only, then you can ignore this line.
}
)
print(r.text)
"""
Type
r.text: str
r.content: bytes
"""
# If you want to save it as a file, then you can try as below.
# f=open('temp.ico','wb')
# f.write(r.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment