2016-07-01 16 views
0
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')) 

這是給我有人可以給一個python請求在github上傳一個發佈資產的例子嗎?

>>> r.text 
u'{"message":"Not Found","documentation_url":"https://developer.github.com/enterprise/2.4/v3"}' 

我要去哪裏錯了?

+0

可能是一個url錯誤,根據該文檔鏈接也可能是auth錯誤 – nthall

回答

2

所以我會建議前言本,如果你使用一個庫是那麼容易,因爲:

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')) 

考慮到這一點,我會還與前言本「應用程序/二進制」不一個真正的媒體類型(參見:https://www.iana.org/assignments/media-types/media-types.xhtml

接下來,如果你read the documentation,你會發現,GitHub的要求有真正 SNI(服務器名稱指示)客戶端,所以根據您的Python版本,你也可以必須安裝pyOpenSSL,pyasn1和來自PyPI的ndg-httpsclient

我不知道什麼是URL看起來像是企業的實例,但對於公共GitHub上,它看起來像:

https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets?name=foo.sh 

那麼你將有一個爲url,再加上你會想要你的身份驗證憑據(在你的情況下,你似乎想要使用基本身份驗證)。然後,你會想在頭一個有效媒體類型,例如,

headers = {'Content-Type': 'text/plain'} 

,您的電話看起來幾乎完全正確:

requests.post(url, headers=headers, data=open('file.txt', 'rb'), auth=(username, password)) 

爲了得到正確的URL,你應該做的:

release = requests.get(release_url, auth=(username, password)) 
upload_url = release.json().get('upload_url') 

注意這是一個URITemplate。您需要刪除模板或使用類似uritemplate.py的庫來解析它並使用它爲您構建網址。

最後一個提示,github3.py(原始示例中的庫)爲您處理所有這些問題。

相關問題