Question

GitHub Search Authorization

I am trying to authorize my requests to the search feature within the GitHub API. I generated a classic token with all permissions unchecked (since I am only searching through public repos). This token however doesn't seem to work as I get a 'Bad Credentials' error.

I tried the following -


import requests

headers = {
    'Authorization': 'token < MY_TOKEN >',
    'X-GitHub-Api-Version': '2022-11-28',
}

response = requests.get('https://api.github.com/rate_limit', headers=headers, proxies = custom_proxies)

print(response.content) 

I then get the following error-

b'{"message":"Bad credentials","documentation_url":"https://docs.github.com/rest","status":"401"}'
 2  31  2
1 Jan 1970

Solution

 0

The bad credentials error indicates that the token you're using is invalid or not recognized.

Can you please check your API token here https://github.com/settings/tokens

Ensure the token has at least public_repo permission.

Then I give you a code alternative:

import requests

token = '<MY_TOKEN>' # Write here your generated token

headers = {
    'Authorization': f'token {token}',
    'X-GitHub-Api-Version': '2022-11-28',
}

response = requests.get('https://api.github.com/rate_limit', headers=headers)

print(response.content)

Regards!

2024-07-16
Ikkxeer

Solution

 0

Replace word token with Bearer. Here is the curl command that shows the expected headers:

curl -L \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <YOUR-TOKEN>" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/rate_limit
2024-07-19
Delta George