gpt4 book ai didi

python - 为什么用于内容更新的 Github API 会抛出 400 错误?

转载 作者:行者123 更新时间:2023-12-01 08:34:55 24 4
gpt4 key购买 nike

尝试使用 github API 更新文件。

https://developer.github.com/v3/repos/contents/#update-a-file

url = "https://api.github.com/repos/ishandutta2007/reach-github-issuers/contents/README.md"
r = requests.get(url)
repo_item = json.loads(r.text or r.content)
deco_content = base64.b64decode(repo_item['content'])
appended_str = open('readme_md_template.txt', 'r').read().encode()
deco_content = deco_content + appended_str
updated_content = base64.b64encode(deco_content)

retj = {}
retj["message"] = "Adding donations"
retj["content"] = str(updated_content)
retj["sha"] = repo_item['sha']

headers = {'Authorization': 'token %s' % API_TOKEN}
r = requests.put(url, retj, headers=headers)
print(r)

< Response [400] >

这里是最终的retj变量看起来像

{'content': "b'IyByZWFjaC1naXRodWItaXNzdWVycwoKVGhpcyBpcyBhIHJlcG8gd2hpY2ggY29sbGVjdHMgbWFpbCBpZCBvZiBhbGwgZm9ya2VycyBvZiBhIHJlcG8gYW5kIHNlbmQgdGhlbSBhIHNwZWNpZmljIG1haWwuCgojIyMgVG8gU2V0dXA6CgpPbiBob3cgdG8gc2V0dXAgZ21haWwgQVBJIGZvbGxvdyBbdGhpc10oaHR0cHM6Ly9naXRodWIuY29tL3NoYW5rYXJqNjcvcHl0aG9uLWdtYWlsLWFwaS9ibG9iLzU2M2M3YmY3MjJjNjliZTRmZWQyMjA0ZTI4MjlkMGFiODQzZDg3MjkvUkVBRE1FLm1kI2luc3RhbGwpCgojIyMgVG8gcnVuOgoKMS4gYHB5dGhvbjMgZ2l0aHViLWlzc3VlcnMtY3Jhd2xlci5weSAtLWlzc3VlIElTU1VFX0ZVTExfVVJMYAoKMi4gVGhlIGFib3ZlIHNjcmlwdCB3aWxsIGNyZWF0ZSBlbWFpbC1saXN0LmNzdiB3aXRoIGFsbCBpc3N1ZSBjb2xsYWJvcmF0b3JzJyBkZXRhaWxzLgoKMy4gTW9kaWZ5IHRoZSB0ZW1wbGF0ZXMgYGhlbHBfcmVxdWVzdC5odG1sYCBhcyBwZXIgbmVlZC4gSWYgeW91IG5lZWQgdG8gcmVuYW1lIG9yIHJlY3JlYXRlIG5ldyBodG1sIHRlbXBsYXRlIGZlZWwgZnJlZSB0byBkbyBzbywganVzdCBtYWtlIHN1cmUgeW91IHVwZGF0ZSBuYW1lIG9mIGl0IGluIGBzZW5kX21haW4ucHlgKGFzIG9mIG5vdyBoYXJkY29kZWQpCgo0LiBgcHl0aG9uMyBzZW5kX21haWwucHlgCgojIyMgU3VwcG9ydDoKCklmIHlvdSB3YW50IHRoZSBnb29kIHdvcmsgdG8gY29udGludWUgcGxlYXNlIHN1cHBvcnQgdXMgb24KCiogW1BBWVBBTF0oaHR0cHM6Ly93d3cucGF5cGFsLm1lL2lzaGFuZHV0dGEyMDA3KQoqIFtCSVRDT0lOIEFERFJFU1M6IDNMWmF6S1hHMThIeGEzTExOQWVLWVpOdEx6Q3hwdjFMeURdKGh0dHBzOi8vd3d3LmNvaW5iYXNlLmNvbS9qb2luLzVhOGU0YTA0NWIwMmM0MDNiYzNhOWMwYykK'",
'message': 'Adding donations',
'sha': 'dcb2ea15cfccbf2737b2a2db6df1cd63a61dc626'}

最佳答案

这就是解决问题的方法:

updated_content = base64.b64encode(deco_content)

更改为

updated_content = base64.b64encode(deco_content).decode('utf-8')

并且也进行了更改(但此更改可能没有必要)

r = requests.put(url, retj, headers=headers)

r = requests.put(url, data = json.dumps(retj), headers=headers)

关于python - 为什么用于内容更新的 Github API 会抛出 400 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53771483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com