使用github3.py版本0.9.5 文档,我尝试创建一个存储库对象,但它一直返回Nonetype,因此无法访问存储库的内容。在StackOverflow上似乎没有任何其他文章,也没有关于库中解决这个问题的GitHub问题的对话。
AttributeError: 'NoneType' object has no attribute 'contents'是我所收到的准确错误。
在上面写着repo = repository('Django', auth)的线路上,我尝试用fv4更改auth,但这并没有改变其他任何东西。
#!/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)发布于 2016-03-02 15:50:59
所以,您的代码有一些问题,但让我先帮助您解决眼前的问题,然后再继续讨论其他问题。
在您困惑的行中使用的是github3.repository。让我们看看这个特定函数的文档 (您也可以通过调用help(repository)来查看它)。您将看到repository需要两个参数owner和repository,并将它们描述为存储库的所有者和存储库本身的名称。所以在你的用法中
repo = repository('Django', 'Django')但你的认证证书在哪里..。还有一件事,你在做
fv4 = login(username, password, token)您只需要指定其中一些参数。如果要使用令牌,请执行以下操作
fv4 = login(token=token)或者如果您想使用基本身份验证
fv4 = login(username, password)两者都会运作得很好。如果您想继续进行身份验证,那么可以这样做。
repo = fv4.repository('Django', 'Django')因为fv4是一个GitHub对象,它是文档化的这里,repository函数在所有东西下面都使用它。
所以这应该能帮助你克服大部分的问题。
注意,在github3.py的文档示例中,我们通常调用login() gh的结果。这是因为gh只是一个存储凭证的GitHub对象。它不是你的用户或类似的东西。这将是(在您的github3.py版本中) fv4 = gh.user()。(如果其他人正在阅读这篇文章,并使用github3.py 1.0版本(目前正在发布前),那么它就是fv4 = gh.me()。
https://stackoverflow.com/questions/35733470
复制相似问题