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

如何用API设置GitHub仓库Custom Properties值?遇404错误求助

解决GitHub仓库自定义属性设置404问题

你的代码存在三个核心问题导致404错误:

  • 请求端点错误:你当前使用的是/repos/{github_Org}/{repo_name}/secret-scanning/alerts(这是Secret Scanning告警的接口),而设置仓库自定义属性的正确PUT端点应为 /repos/{github_Org}/{repo_name}/properties
  • 缺少必要请求头:发送JSON格式请求体时,必须添加Content-Type: application/json头,否则GitHub API无法正确解析你的请求内容
  • Payload生成方式不严谨:直接拼接JSON字符串容易出现语法错误,建议用json.dumps()生成标准JSON格式内容

修正后的代码示例:

import os
import json
import http.client

headers = {
    "Accept": "application/vnd.github+json",
    "Authorization": "Bearer " + os.getenv('GITHUB_APIKEY'),
    "User-Agent": "python script",
    "X-GitHub-Api-Version": "2022-11-28",
    "Content-Type": "application/json"  # 新增该请求头
}
# 替换为正确的自定义属性接口端点
request_url = f"/repos/{github_Org}/{repo_name}/properties"
conn = http.client.HTTPSConnection("api.github.com")

# 用字典定义属性结构,再转为标准JSON字符串
custom_properties = {
    "properties": [
        {"property_name": "Owner-of-repo", "value": "BH"},
        {"property_name": "Intented-Purpose", "value": "Open-Source"}
    ]
}
payload = json.dumps(custom_properties)

conn.request("PUT", request_url, payload, headers)
response = conn.getresponse()
response_data = response.read()
conn.close()
json_data = json.loads(response_data.decode("utf-8"))

额外注意:你的API密钥需要拥有对应仓库的repo(私有仓库)或public_repo(公开仓库)权限,确保权限足够完成属性设置操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 20:24:53