所以,这是我尝试使用的行:
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
您需要create a personal access token第一的。例如:
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 library :
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"
)
关于json - 我可以使用 python requests 在 github 上发布 .json 文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62353797/