Home > Software engineering >  Github API works in curl but not python - bad credentials
Github API works in curl but not python - bad credentials

Time:02-15

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!

CodePudding user response:

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)

CodePudding user response:

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)
  • Related