jbug123
jbug123

Reputation: 33

Github API works in curl but not python - bad credentials

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!

Upvotes: 1

Views: 1063

Answers (2)

Tushar Deshmukh
Tushar Deshmukh

Reputation: 11

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

Upvotes: 1

David Butler
David Butler

Reputation: 313

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)

Upvotes: 0

Related Questions