You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyGithub编辑已创建Gist时遇404错误,求解决方法

PyGithub编辑Gist触发404 Not Found错误的解决办法

你的问题代码:

import github.InputFileContent
from github import Github

g = Github("priv key")

gist = g.get_gist("004e205c856d5f934704333f5725d61e")
gist.edit(
    "Description edited by PyGithub",
    {"gistfile1.txt": github.InputFileContent("File also created by PyGithub")},
)

运行后出现的错误:

Traceback (most recent call last):
  File "/Users/boazburnett/Library/Application Support/JetBrains/PyCharmCE2022.2/scratches/scratch.py", line 9, in <module>
    {"gistfile1.txt": github.InputFileContent("File also created by PyGithub")},
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/Gist.py", line 259, in edit
    "PATCH", self.url, input=post_parameters
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/Requester.py", line 355, in requestJsonAndCheck
    verb, url, parameters, headers, input, self.__customConnection(url)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/Requester.py", line 378, in __check
    raise self.__createException(status, responseHeaders, output)
github.GithubException.UnknownObjectException: 404 {"message": "Not Found", "documentation_url": "https://docs.github.com/rest/reference/gists#update-a-gist"}

可能的原因及解决步骤

  • Gist ID错误或Gist已被删除
    核对代码中的Gist ID与目标Gist网页URL中的ID是否完全一致,直接访问该Gist的网页链接,确认它存在且你有权限访问。

  • 个人访问令牌(PAT)权限不足
    用于认证的令牌必须包含gist权限。进入GitHub账号设置页面,找到对应的令牌,检查是否勾选了gist权限。如果未勾选,重新生成一个带该权限的令牌替换代码中的"priv key"。

  • PyGithub版本兼容问题
    旧版本PyGithub可能与GitHub API存在适配问题,执行以下命令升级到最新版本:

    pip install --upgrade PyGithub
    
  • 验证Gist基础访问权限
    在调用edit方法前,先尝试打印Gist的基础信息做测试,比如:

    print(gist.description)
    

    如果这一步就触发404错误,说明问题出在Gist的可访问性或令牌权限上,而非edit方法的参数格式。

内容的提问来源于stack exchange,提问作者Boaztheostrich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 17:25:51