1

I am trying to use the Github API and it works when I do curl from terminal but not when using the Python Request library:

WORKS:

curl -v --location --request GET 'https://api.github.com/search/repositories?q=stars:>=500+created:2017-01-01&order=asc&per_page=100' \
--header 'Authorization: Bearer MY_TOKEN'

DOESN'T WORK:

import requests

url = https://api.github.com/search/repositories?q=stars:>=500+created:2017-01-01&order=asc&per_page=100
r = requests.get(url, auth=(MY_GITHUB_USERNAME, MY_TOKEN))

I get the following response for python:

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

Would appreciate any help - thank you in advance!

2 Answers 2

1

Please try with below snippet, you have to send PAT in header,

import requests

url = "https://api.github.com/search/repositories?q=stars:>=500+created:2017-01-01&order=asc&per_page=100"

payload={}
headers = {
  'Authorization': 'Bearer MY_TOKEN',
  
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

Check the documentation https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

The following should get you going I think. Think it is just the headers that needed a little bit of a change.

import requests

token = MYTOKEN
url = "some url"

headers = {"Accept": "application/vnd.github.html",
           "Authorization": f"token {token}",}

request = requests.get(url, headers=headers)

Comments

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.