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

Bitbucket REST API请求Python失败但curl成功,求排查原因

curl请求Bitbucket API成功,Python requests却返回401认证失败

我通过Bitbucket REST API获取信息时,curl命令能正常返回200状态码和目标内容,但用Python的requests库发送相同请求却返回401状态码,提示认证失败。以下是相关信息:

curl请求命令及响应

C:\>curl -vv -k --header "Authorization: Bearer %TOKEN%" https://bitbucket.cicd/rest/api/1.0/projects/%OWNER%/repos/%PROJECT%/raw/%LOCATION%?at=%COMMIT%
*   Trying 10.<....>:443...
* Connected to bitbucket.cicd (10.<....>) port 443 (#0)
* schannel: disabled automatic use of client certificate
* ALPN: offers http/1.1
* ALPN: server did not agree on a protocol. Uses default.
* using HTTP/1.x
> GET /rest/api/1.0/projects/<.....> HTTP/1.1
> Host: bitbucket.cicd
> User-Agent: curl/8.0.1
> Accept: */*
> Authorization: Bearer <token>
>
* schannel: failed to decrypt data, need more data
< HTTP/1.1 200
< x-arequestid: *<....>
< set-cookie: BITBUCKETSESSIONID=<....>; Max-Age=1209600; Expires=Sat, 15 Jul 2023 15:25:03 GMT; Path=/; Secure; HttpOnly
< x-auserid: <....>
< x-ausername: <....>
< x-asessionid: <....>
< cache-control: private, no-cache
< pragma: no-cache
< cache-control: no-cache, no-transform
< vary: x-ausername,x-auserid,cookie,accept-encoding
< x-content-type-options: nosniff
< content-disposition: attachment; filename="<....>"; filename*=UTF-8''<....>
< content-type: text/plain;charset=UTF-8
< content-length: 3916
< date: Sat, 01 Jul 2023 15:25:02 GMT
<
<...file content...>
* Connection #0 to host bitbucket.cicd left intact

Python代码

import requests

TOKEN = '<token>'
OWNER = '<owner>'
PROJECT = '<project>'
LOCATION = '<location>'
COMMIT = '<commit>'

headers = {
    'Authorization': f'Bearer {TOKEN}'
    }

url = f'https://bitbucket.cicd/rest/api/1.0/projects/{OWNER}/repos/{PROJECT}/raw/{LOCATION}?at={COMMIT}'
response = requests.get(url=url, headers=headers, verify=False)
print(response)

错误响应

response.status_code: 401
response._content: b'{"errors":[{"context":null,"message":"Authentication failed. Please check your credentials and try again.","exceptionName":"com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException"}]}'

可能的原因及解决方法

1. Token存在空白字符或未正确处理

如果你的TOKEN包含前后空格、换行符,Python的f-string拼接时会保留这些字符,而curl通过环境变量调用时会自动忽略空白。可以尝试去除TOKEN的空白字符:

headers = {
    'Authorization': f'Bearer {TOKEN.strip()}'
}

2. 请求头部不一致

requests默认会添加User-Agent: python-requests/xxx等头部,部分Bitbucket服务器可能对请求头部的完整性有校验。模拟curl的头部信息:

headers = {
    'Authorization': f'Bearer {TOKEN}',
    'User-Agent': 'curl/8.0.1',
    'Accept': '*/*'
}

3. URL参数未编码

curl会自动对URL中的特殊字符(如空格、斜杠、特殊符号)进行URL编码,而Python直接拼接字符串时不会处理。对路径参数进行编码:

from requests.utils import quote

encoded_location = quote(LOCATION)
encoded_commit = quote(COMMIT)
url = f'https://bitbucket.cicd/rest/api/1.0/projects/{OWNER}/repos/{PROJECT}/raw/{encoded_location}?at={encoded_commit}'

4. 会话Cookie缺失

curl请求中服务器返回了BITBUCKETSESSIONID Cookie,部分Bitbucket实例可能要求请求携带会话标识。使用requests的Session对象保持会话:

session = requests.Session()
session.verify = False
session.headers.update({'Authorization': f'Bearer {TOKEN}'})
response = session.get(url)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 16:05:35