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

使用Python调用Bitbucket API启用YAAC遇400错误,请求排查

问题修复方案

你的代码存在语法、数据格式等多处问题,导致Bitbucket API无法正常解析请求Payload,以下是具体修正点及修复后的代码:

1. Python字典语法错误

  • payload 定义缺少赋值符号 =,需写成 payload = { ... }
  • scope 字段缺少冒号,正确写法为 "scope": { ... }

2. 布尔值格式错误

enabled 和 configured 字段需要传入原生布尔值 True,而非字符串 "true",Bitbucket API 仅接受标准JSON布尔类型。

3. 请求参数传递错误

发送JSON格式请求时,应使用 json=payload 参数替代 data=payload,requests 库会自动完成JSON序列化,避免手动处理格式问题。

4. 变量拼写与占位符问题

  • resourceld 是拼写错误,正确字段名为 resourceId,且需提前定义该变量的具体值(如目标仓库ID)
  • URL中的 ${GITURL}、${projectName}、${repoName} 需替换为实际的Bitbucket地址、项目键和仓库标识,或用Python字符串格式化填充。

修复后的完整代码

import requests

def stash_status():
    # 替换为实际的配置信息
    auth_token = "your_bearer_token"
    resource_id = 123  # 目标仓库的ID
    git_url = "your_bitbucket_domain"
    project_name = "your_project_key"
    repo_name = "your_repo_slug"

    headers = {
        "Authorization": f'Bearer {auth_token}',
        "Accept": "application/json",
        "Content-Type": "application/json"
    }

    payload = {
        "key": "com.isroot.stash.plugin.yacc:yaccHook",
        "name": "Yet Another Commit Checker",
        "type": "PRE_RECEIVE",
        "description": "Yet Another Commit Checker pre-receive hook",
        "configFormKey": "com.isroot.stash.plugin.yacc:yaacHook-config-form",
        "scopeTypes": [
            "REPOSITORY",
            "PROJECT"
        ],
        "enabled": True,
        "configured": True,
        "scope": {
            "type": "REPOSITORY",
            "resourceId": resource_id
        }
    }

    api_url = f"https://{git_url}/rest/api/latest/projects/{project_name}/repos/{repo_name}/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled"
    response = requests.put(api_url, verify=False, headers=headers, json=payload)

    print(response.status_code)
    print(response.content)

stash_status()

额外注意事项

  • verify=False 仅适合测试环境,生产环境建议保留证书验证,避免安全风险
  • 确认Bearer Token拥有修改仓库钩子设置的权限
  • 若仍有错误,可使用 response.json() 查看API返回的详细错误信息

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 15:07:28