【问题标题】:Simple python request to update GitHub file更新 GitHub 文件的简单 python 请求
【发布时间】:2020-11-07 09:19:58
【问题描述】:

我没有看到很多关于这个主题的好资源,但我发现我设法制作了这个脚本来更新一个简单的文本文件,使其只有一个 1:

payload = {
  "message": "update file.txt",
  "committer": {
    "name": "<name>",
    "email": "<email>"
  },
  "content": "1",
  "sha": "<sha>"
}

url = "https://api.github.com/repos/<username>/<repo>/contents/file.txt"
token = '<token>'
usern = '<username>'

rget = requests.get(url)
print(rget)

rput = requests.put(url, auth=(usern, token), data=json.dumps(payload))
print(rput)

get 请求有效,但 put 请求给出 422 错误。知道如何解决该错误吗?

【问题讨论】:

    标签: python api github python-requests github-api


    【解决方案1】:

    发现问题:需要对消息进行编码。用过这个:

    message = "1"
    message_bytes = message.encode("ascii")
    content = base64.b64encode(message_bytes)
    

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 2015-02-16
      • 1970-01-01
      • 2017-08-30
      • 2017-12-29
      • 2018-10-15
      • 1970-01-01
      • 2015-12-28
      • 2018-07-05
      相关资源
      最近更新 更多