Ank*_*wal 5 python github github-api
url = 'https://github.abc.defcom/api/v3/repos/abc/def/releases/401/assets?name=foo.sh'
r = requests.post(url, headers={'Content-Type':'application/binary'}, data=open('sometext.txt','r'), auth=('user','password'))
Run Code Online (Sandbox Code Playgroud)
这给了我
>>> r.text
u'{"message":"Not Found","documentation_url":"https://developer.github.com/enterprise/2.4/v3"}'
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
因此,我将首先提出以下建议,即如果您使用库,它就像以下一样简单:
from github3 import GitHubEnterprise
gh = GitHubEnterprise(token=my_token)
repository = gh.repository('abc', 'def')
release = repository.release(id=401)
asset = release.upload_asset(content_type='application/binary', name='foo.sh', asset=open('sometext.txt', 'rb'))
Run Code Online (Sandbox Code Playgroud)
考虑到这一点,我还将以“应用程序/二进制”不是真正的媒体类型作为开头(请参阅:https : //www.iana.org/assignments/media-types/media-types.xhtml)
接下来,如果你阅读文档,你会发现,GitHub的要求有客户真正SNI(服务器名称指示),所以根据您的Python版本,你也可能需要安装pyOpenSSL,pyasn1以及ndg-httpsclient一封来自PyPI。
我不确定企业实例的 URL 是什么样的,但对于公共 GitHub,它看起来像:
https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets?name=foo.sh
Run Code Online (Sandbox Code Playgroud)
因此,您将拥有它作为url,而且您将需要您的身份验证凭据(在您的情况下,您似乎想要使用基本身份验证)。然后你会想要一个有效的媒体类型在标题中,例如,
headers = {'Content-Type': 'text/plain'}
Run Code Online (Sandbox Code Playgroud)
你的电话看起来几乎完全正确:
requests.post(url, headers=headers, data=open('file.txt', 'rb'), auth=(username, password))
Run Code Online (Sandbox Code Playgroud)
要获得正确的网址,您应该执行以下操作:
release = requests.get(release_url, auth=(username, password))
upload_url = release.json().get('upload_url')
Run Code Online (Sandbox Code Playgroud)
请注意,这是一个 URITemplate。您需要删除模板或使用类似的库uritemplate.py来解析它并使用它来为您构建 URL。
最后提醒一下,github3.py(原始示例中的库)会为您处理所有这些。