Search code examples
pythongithubpygithub

Getting Github Repo Filenames Using Python


How do you get a list of all filenames in a Github repo using Python/PyGithub without logging into a user account?

How can I use github.Github(self.login, self.password) if I only know about a user name, not his/her password?


Solution

  • The source code shows that you can keep the password to 'None' when it comes to get a GitHub object.

        def __init__(self, login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page, api_preview):
            self._initializeDebugFeature()
    
            if password is not None:
                login = login_or_token
                if atLeastPython3:
                    self.__authorizationHeader = "Basic " + base64.b64encode((login + ":" + password).encode("utf-8")).decode("utf-8").replace('\n', '')  # pragma no cover (Covered by Authentication.testAuthorizationHeaderWithXxx with Python 3)
                else:
                    self.__authorizationHeader = "Basic " + base64.b64encode(login + ":" + password).replace('\n', '')
            elif login_or_token is not None:
                token = login_or_token
                self.__authorizationHeader = "token " + token
            else:
                self.__authorizationHeader = None
    

    That is tested here:

    def testLoggingWithoutAuthentication(self):
       self.assertEqual(github.Github().get_user("jacquev6").name, "Vincent Jacques")
    

    From there, you can get any public repo and list their files.

    You only need to "login" for private repos.