0

使用 github3.py 版本 0.9.5文档,我正在尝试创建一个存储库对象,但它不断返回Nonetype,因此我无法访问存储库的内容。StackOverflow 上似乎没有任何其他帖子,或者图书馆的 GitHub 问题上的对话解决了这个问题。

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

在说repo = repository('Django', 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)
4

1 回答 1

1

因此,您的代码有一些问题,但让我先帮助您解决当前的问题,然后再讨论其他问题。

您正在使用github3.repository您感到困惑的线路。让我们看一下该特定函数的文档(您也可以通过调用来查看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记录的对象,并且该函数在所有内容下都使用该对象。repository

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


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

于 2016-03-02T15:50:59.933 回答