json - 我可以使用 python requests 在 github 上发布 .json 文件吗?

标签 json github python-requests

所以,这是我尝试使用的行:

 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/

相关文章:

json - 违反mapKeysMonotonic前提条件

json - 如何检查jq中是否有数组或对象?

ios - 如何使用swift在折线图中显示json数据

xcode - 从 github 克隆 repo 到 xcode 问题

linux - 如果 git 仓库已经存在,我可以克隆它吗?

json - 在 python 中解析 A​​lexa json 响应以获取值名称时出现问题

jenkins - 如何在某个分支中推送时设置 github webhook 触发器

python - 从 td 标签中抓取特定数据

networking - 一台机器可以从多个不同的IP地址发出请求吗?

python - 在 webdriver 和请求之间复制 cookie 时出错