因此,这是我尝试使用的行:
r= requests.post('https://github.com/Gevez/gameficacao/upload/master', auth=HTTPBasicAuth('user', 'pass'), data='data.json')
即使我正确输入了凭据,服务器也会返回403代码。现在我想知道我是用错误的方式做的还是github不允许我做的。
您可以使用Github create repo content API通过API在存储库上创建文件:
PUT /repos/:owner/:repo/contents/:path
您首先需要创建一个个人访问令牌。例如 :
import requests
import base64
token = "YOUR_TOKEN"
repo = 'bertrandmartel/test-repo'
path = 'data.json'
data = open("data.json", "r").read()
r = requests.put(
f'https://api.github.com/repos/{repo}/contents/{path}',
headers = {
'Authorization': f'Token {token}'
},
json = {
"message": "add new file",
"content": base64.b64encode(data.encode()).decode(),
"branch": "master"
}
)
print(r.status_code)
print(r.json())
您还可以使用PyGithub库:
from github import Github
token = "YOUR_TOKEN"
repo = "bertrandmartel/test-repo"
path = "data.json"
# if using username and password
#g = Github("user", "password")
g = Github(token)
data = open("data.json", "r").read()
repo = g.get_repo(repo)
repo.create_file(
path = path,
message = "add new file",
content = data,
branch = "master"
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句