Access Github API using Personal Access Token with Python urllib2 Access Github API using Personal Access Token with Python urllib2 python python

Access Github API using Personal Access Token with Python urllib2


I don't know why this question was marked down. Anyway, I found an answer:

from urllib2 import urlopen, Requesturl = "https://api.github.com/users/vhf/repos"token = "my_personal_access_token"request = Request(url)request.add_header('Authorization', 'token %s' % token)response = urlopen(request)print(response.read())


I realize this question is a few years old, but if anyone wants to auth with a personal access token while also using the requests.get and requests.post methods you also have the option of using the method below:

request.get(url, data=data, auth=('user','{personal access token}')) 

This is just basic authentication as documented in the requests library, which apparently you can pass personal access tokens to according to the github api docs.

From the docs:

Via OAuth Tokens Alternatively, you can use personal access tokens orOAuth tokens instead of your password.

curl -u username:token https://api.github.com/user

This approach is useful if your tools only support Basic Authentication but you want to take advantage of OAuth access token security features.