Pygithub无法使用用户名和密码登录,如何解决?

问题描述

使用pygithub无法使用登录名和密码登录GitHub

错误

Traceback (most recent call last):
  File "D:\Dev\PetProjects\PythonPetProjects\OneClickGitHub\oneclickgithub\main.py",line 31,in <module>
    repo = g.get_user().get_repo(github_creds['rep'])
  File "C:\Users\PANDEMIC\AppData\Local\pypoetry\Cache\virtualenvs\oneclickgithub-UDpxgjZD-py3.9\lib\site-packages\github\AuthenticatedUser.py",line 1019,in get_repo
    "GET",f"/repos/{self.login}/{name}"
  File "C:\Users\PANDEMIC\AppData\Local\pypoetry\Cache\virtualenvs\oneclickgithub-UDpxgjZD-py3.9\lib\site-packages\github\AuthenticatedUser.py",line 231,in login
    self._completeIfNotSet(self._login)
  File "C:\Users\PANDEMIC\AppData\Local\pypoetry\Cache\virtualenvs\oneclickgithub-UDpxgjZD-py3.9\lib\site-packages\github\GithubObject.py",line 297,in _completeIfNotSet
    self._completeIfNeeded()
  File "C:\Users\PANDEMIC\AppData\Local\pypoetry\Cache\virtualenvs\oneclickgithub-UDpxgjZD-py3.9\lib\site-packages\github\GithubObject.py",line 301,in _completeIfNeeded
    self.__complete()
  File "C:\Users\PANDEMIC\AppData\Local\pypoetry\Cache\virtualenvs\oneclickgithub-UDpxgjZD-py3.9\lib\site-packages\github\GithubObject.py",line 308,in __complete
    headers,data = self._requester.requestJsonAndCheck("GET",self._url.value)
  File "C:\Users\PANDEMIC\AppData\Local\pypoetry\Cache\virtualenvs\oneclickgithub-UDpxgjZD-py3.9\lib\site-packages\github\Requester.py",line 353,in requestJsonAndCheck
    return self.__check(
  File "C:\Users\PANDEMIC\AppData\Local\pypoetry\Cache\virtualenvs\oneclickgithub-UDpxgjZD-py3.9\lib\site-packages\github\Requester.py",line 378,in __check
    raise self.__createException(status,responseHeaders,output)
github.GithubException.GithubException: 401 {"message": "Requires authentication","documentation_url": "https://docs.github.com/rest/reference/users#get-the-authenticated-user"}

这是代码

import os
import sys
import json
import requests


from github import Github


def get_files(dir_name: str = '.') -> list:
    list_of_files = os.listdir(dir_name)
    complete_file_list = list()
    for file in list_of_files:
        complete_filepath = os.path.join(dir_name,file)
        if os.path.isdir(complete_filepath):
            complete_file_list = complete_file_list + get_files(complete_filepath)
        else:
            complete_file_list.append(complete_filepath)

    return complete_file_list

def get_data_from_json(filepath: str) -> dict:
    with open(filepath,encoding='utf-8') as json_file:
        return json.load(json_file)


github_creds = get_data_from_json('creds.json')

g = Github(github_creds['login'],github_creds['password'])

repo = g.get_user().get_repo(github_creds['rep'])
all_files = []
try:
    contents = repo.get_contents("")
    while contents:
        file_content = contents.pop(0)
        if file_content.type == "dir":
            contents.extend(repo.get_contents(file_content.path))
        else:
            file = file_content
            all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
except:
    print('Repository is empty')



def get_file_content(filepath: str) -> str:
    with open(filepath,'r') as file:
        return file.read()    

files = get_files('.')

for file in files:
    current_file_content = get_file_content(file)
    if file in all_files:
        contents = repo.get_contents(file)
        repo.update_file(contents.path,"committing files",current_file_content,contents.sha,branch=github_creds['branch'])
        print(file + ' UPDATED')
    else:
        repo.create_file(file,branch=github_creds['branch'])
        print(file + ' CREATED')

看起来你的帖子主要是代码;请添加更多详细信息。看起来您的帖子主要是代码;请添加更多详细信息。看起来您的帖子主要是代码;请添加更多详细信息。看起来您的帖子主要是代码;请添加更多详细信息。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)