Here's some code that might help you out.
Examples:
Example 1 (auth):
username = 'user'
token = 'token'
login = requests.get('https://api.github.com/search/repositories?q=github+api', auth=(username,token))
Example 2 (headers):
headers = {'Authorization': 'token ' + token}
login = requests.get('https://api.github.com/user', headers=headers)
print(login.json())
Example 3 (delete repo):
user = 'username'
repo = 'some_repo' # Delete this repo
headers = {'Authorization': 'token ' + token}
login = requests.delete('https://api.github.com/' + 'repos/' + user + '/' + repo, headers=headers)
Example 4 (create repo):
repo = 'some_repo'
description = 'Created with api'
payload = {'name': repo, 'description': description, 'auto_init': 'true'}
login = requests.post('https://api.github.com/' + 'user/repos', auth=(user,token), data=json.dumps(payload))
You might want to take a look at the following docs:
Requests Docs
Github API docs
I hope this helps.
Answer from user6927293 on Stack OverflowHere's some code that might help you out.
Examples:
Example 1 (auth):
username = 'user'
token = 'token'
login = requests.get('https://api.github.com/search/repositories?q=github+api', auth=(username,token))
Example 2 (headers):
headers = {'Authorization': 'token ' + token}
login = requests.get('https://api.github.com/user', headers=headers)
print(login.json())
Example 3 (delete repo):
user = 'username'
repo = 'some_repo' # Delete this repo
headers = {'Authorization': 'token ' + token}
login = requests.delete('https://api.github.com/' + 'repos/' + user + '/' + repo, headers=headers)
Example 4 (create repo):
repo = 'some_repo'
description = 'Created with api'
payload = {'name': repo, 'description': description, 'auto_init': 'true'}
login = requests.post('https://api.github.com/' + 'user/repos', auth=(user,token), data=json.dumps(payload))
You might want to take a look at the following docs:
Requests Docs
Github API docs
I hope this helps.
For one, I would recommend using a wrapper for the API. You're asking a lot of questions on here that could be simplified by finding a wrapper whose API you appreciate. There's a list of wrappers written in Python here.
As for your actually answering your question, the GitHub documentation is fairly clear that you need to send the Authorization header. Your call would actually look like this:
self.headers = {'Authorization': 'token %s' % self.api_token}
r = requests.post(url, headers=self.headers)
Since it seems like you're using requests and a class, might I be so bold as to make a recommendation? Let's say you're doing something like making a client for the API. You might have a class like so:
class GitHub(object):
def __init__(self, **config_options):
self.__dict__.update(**config_options)
self.session = requests.Session()
if hasattr(self, 'api_token'):
self.session.headers['Authorization'] = 'token %s' % self.api_token
elif hasattr(self, 'username') and hasattr(self, 'password'):
self.session.auth = (self.username, self.password)
def call_to_the_api(self, *args):
# do stuff with args
return self.session.post(url)
The Session object will take care of the authentication for you (either by the tokens or username and password combination).
Also, if you end up deciding to use github3.py for your API wrapper needs, there's a tag on here for it.
A Python script which can fetch the contents of a GitHub repo using it's URL
How to access the github API via requests in python? - Stack Overflow
PyGithub iterate over public repos, exceeding rate limit too fast
PyGitHub - how to get ALL repos?
This is not the place to ask for support on a particular library, a better avenue might be the library's issues page or r/learnpython if all else fails.
That being said, the direct equivalent to
curl -H "Authorization: Token <token>" https://githubserver/api/v3/repositories?visibility=all
seems to be
ghe_instance = Github(base_url=GHE_API_URL, login_or_token=GHE_ACCESS_TOKEN) ghe_instance.get_repos(visibility='all')
https://github.com/PyGithub/PyGithub/blob/master/github/MainClass.py#L297
More on reddit.comVideos
GitHub's API provides basic data about a repository and its contents but does not offer detailed contents of each file unless you specify it in the path while sending the request.
I was working on a project that required extracting code from public repositories using their URLs, and we couldn't find a reliable method to retrieve the repository contents in JSON format.
So i decided to make a script for it. When you run the script it will ask for a url and then it will return the file structure of that repo and the contents in each file. Planning to make it into a package for py and js.
» pip install PyGithub