2

I am trying to access the github API via requests with python (Answers in similar questions here and here do not help).

Using curl, I am able to get a list of recent commits for a project, e.g.

curl -H "Accept: application/vnd.github.inertia-preview+json Authorization: token a0d42faabef23ab5b5462394373fc133ca107890" https://api.github.com/repos/rsapkf/42/commit 

Trying to use the same setup in python with requests I tried to use

url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json", "Authorization": "token a0d42faabef23ab5b5462394373fc133ca107890"}
r = requests.get(url, headers=headers)

as well as

url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
my_username = "someuser"
my_token  = "a0d42faabef23ab5b5462394373fc133ca107890"
r = requests.get(url, headers=headers, auth=(my_username, my_token))

but in both cases I get the response

{'documentation_url': 'https://docs.github.com/rest',
 'message': 'Bad credentials'}

What am I missing here?

4
  • shameless plug: I wrote a package that makes using github API easier: cmustrudel.github.io/strudel.scraper Commented Oct 19, 2020 at 15:23
  • @Marat Thank you for that suggestion, but it does not seem to work. I get an error requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url... Commented Oct 20, 2020 at 5:39
  • You must not give a token, then it works !?! Commented Oct 20, 2020 at 6:10
  • for certain APIs, e.g. to read from private repositories you have access to, you have to use auth. Also, even for public data, unauthenticated requests are limited to only 60/hour per IP Commented Oct 20, 2020 at 15:32

1 Answer 1

0

No Authentication is required. The following works:

url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
r = requests.get(url, headers=headers)
Sign up to request clarification or add additional context in comments.

1 Comment

it only gives you 60 requests per hour. The problem in the original code was that my_username should be token instead of someuser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.