Bitbucket REST API查询使用python失败,但使用curl成功

9bfwbjaz  于 11个月前  发布在  Python
关注(0)|答案(1)|浏览(163)

我正在尝试使用REST API从bitbucket中检索信息。如果我使用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"}]}'


有人能解释一下为什么吗?
先谢谢你了,-尤里

mrfwxfqh

mrfwxfqh1#

经过一系列的探索和实验,我找到了答案。

import requests
import requests.auth

TOKEN = '<token>'
USER = '<user>'
OWNER = '<opwner>'
PROJECT = '<repo>'
LOCATION = '<path>'
COMMIT = '<commit>'

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

字符串
请注意, 值不是用户电子邮件,而是帐户名。

相关问题