First, I would recommend using a wrapper for the API. You ask a lot of questions here that can be simplified by finding a wrapper whose API you rate. There is a list of wrappers written in Python here .
As for your answer to your question, itβs clear enough in the GitHub documentation that you need to send an authorization header . Your call will 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 queries and a class, can I be brave enough to make a recommendation? Say you are doing something like creating a client for an API. You might have a class like this:
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):
The Session object will take care of authentication for you (either with tokens or with a username and password).
Also, if you decide to use github3.py for your API covers, there is a tag here.
source share