为什么我的存储库对象 return Nonetype 与 github3.py?

Why does my repository object return Nonetype with github3.py?

使用 github3.py 版本 0.9.5 documentation,我正在尝试创建一个存储库对象,但它一直返回 Nonetype,因此我无法访问存储库。 Whosebug 上似乎没有任何其他帖子,也没有关于图书馆 GitHub 问题的对话可以解决此问题。

AttributeError: 'NoneType' object has no attribute 'contents' 是我收到的确切错误。

在显示 repo = repository('Django', auth) 的行中,我尝试将 auth 更改为 fv4 但这并没有改变任何其他内容。

#!/usr/bin/env python

from github3 import authorize, repository, login
from pprint import PrettyPrinter as ppr
import github3
from getpass import getuser

pp = ppr(indent=4)


username = 'myusername'
password = 'mypassword'
scopes = ['user', 'repo', 'admin:public_key', 'admin:repo_hook']
note = 'github3.py test'
note_url = 'http://github.com/FreddieV4'

print("Attemping authorization...")


token = id = ''
with open('CREDENTIALS.txt', 'r') as fi:
    token = fi.readline().strip()
    id = fi.readline().strip()


print("AUTH token {}\nAUTH id {}\n".format(token, id))


print("Attempting login...\n")
fv4 = login(username, password, token=token)
print("Login successful!", str(fv4), '\n')


print("Attempting auth...\n")
auth = fv4.authorization(id)
print("Auth successful!", auth, '\n')


print("Reading repo...\n")
repo = repository('Django', auth)
print("Repo object...{}\n\n".format(dir(repo)))


print("Repo...{}\n\n".format(repo))
contents = repo.contents('README.md')


pp.pprint('CONTENTS {}'.format(contents))


contents.update('Testing github3.py', contents)

#print("commit: ", commit)

所以您的代码有一些问题,但让我先帮助您解决眼前的问题,然后再处理其他问题。

您在感到困惑的行中使用了 github3.repository。让我们看一下该特定函数的 documentation(您也可以通过调用 help(repository) 来查看)。您会看到 repository 需要两个参数 ownerrepository 并将它们描述为存储库的所有者和存储库本身的名称。所以在你的用法中你会做

repo = repository('Django', 'Django')

但是,您的身份验证凭据放在哪里...好吧,这是另一件事,您正在做

fv4 = login(username, password, token)

您只需指定其中的一些参数。如果您想使用令牌,请执行

fv4 = login(token=token)

或者如果您想使用基本身份验证

fv4 = login(username, password)

两者都可以正常工作。如果您想继续进行身份验证,则可以执行

repo = fv4.repository('Django', 'Django')

因为 fv4 是一个 GitHub 对象,它被记录在案 here 并且 repository 函数在所有内容中使用它。

所以这应该可以帮助您解决大部分问题。


请注意,在 github3.py 的文档示例中,我们通常将 login() 的结果称为 gh。这是因为 gh 只是一个存储了凭据的 GitHub 对象。它不是您的用户或类似的东西。那将是(在您的 github3.py 版本上)fv4 = gh.user()。 (如果其他人正在阅读本文并使用 github3.py 1.0 版本(目前处于预发布阶段),那么它将是 fv4 = gh.me()。)