1

Hello so I am receiving this error BadCredentialsException: 401 {"message": "Bad credentials", "documentation_url": "https://docs.github.com/rest"} when attempting to call a specific repository. I am using jupyter right now because VS code was not working well with github.

Below is my current code:

from github import Github

#Authenticate with GitHub
g = Github('<PAT>') #Replace <PAT> with your personal access token

#Get the repository
repo = g.get_repo('los-lobos') #Replace 'owner/repo' with the name of your repository

# Determine which actions have permissions set on the repo
actions = repo.get_actions()
if not actions.permissions:
    print('No actions have permissions set on the repo')
else:
    for action in actions.permissions:
        print(f'{action} has permissions set on the repo')

# Determine if pipelines are configured for commits
pipelines = repo.get_workflows()
if not pipelines.totalCount:
    print('No pipelines are configured for commits')
else:
    print(f'{pipelines.totalCount} pipelines are configured for commits')

# Determine if new branches are configured for actions on commits
branch_protection = repo.get_branch_protection('main') #Replace 'main' with the name of your default branch
if not branch_protection.restrictions:
    print('New branches are not configured for actions on commits')
else:
    print('New branches are configured for actions on commits')

I used my actual token above too. If anyone could please assist it would be much appreciated.

I installed PyGithub first as recommended in order to call github to the system.

This is the full error message:

BadCredentialsException                   Traceback (most recent call last)
Input In [4], in <cell line: 7>()
      4 g = Github('-----------------------') #Replace <PAT> with your personal access token
      6 #Get the repository
----> 7 repo = g.get_repo('los-lobos') #Replace 'owner/repo' with the name of your repository
      9 # Determine which actions have permissions set on the repo
     10 actions = repo.get_actions()

File ~\anaconda3\lib\site-packages\github\MainClass.py:321, in Github.get_repo(self, full_name_or_id, lazy)
    317 if lazy:
    318     return Repository.Repository(
    319         self.__requester, {}, {"url": url}, completed=False
    320     )
--> 321 headers, data = self.__requester.requestJsonAndCheck("GET", url)
    322 return Repository.Repository(self.__requester, headers, data, completed=True)

File ~\anaconda3\lib\site-packages\github\Requester.py:398, in Requester.requestJsonAndCheck(self, verb, url, parameters, headers, input)
    397 def requestJsonAndCheck(self, verb, url, parameters=None, headers=None, input=None):
--> 398     return self.__check(
    399         *self.requestJson(
    400             verb, url, parameters, headers, input, self.__customConnection(url)
    401         )
    402     )

File ~\anaconda3\lib\site-packages\github\Requester.py:423, in Requester.__check(self, status, responseHeaders, output)
    421 output = self.__structuredFromJson(output)
    422 if status >= 400:
--> 423     raise self.__createException(status, responseHeaders, output)
    424 return responseHeaders, output

BadCredentialsException: 401 {"message": "Bad credentials", "documentation_url": "https://docs.github.com/rest"}
Jayy Griff
  • 19
  • 3
  • it looks like `get_repo` expects the `owner/repo` not just the `repo` – Joran Beasley Apr 05 '23 at 04:24
  • @JoranBeasley I tried putting my username before the repo and it gave me the same exact message – Jayy Griff Apr 05 '23 at 17:13
  • are you sure you are using the right token? im not familliar with the github library... but to me that says you either have an incorrect token, or you are not trying to access the correct repo ... – Joran Beasley Apr 05 '23 at 17:41

0 Answers0