Unable to download from bitbucket cloud with Python requests

Curl works perfectly in git bash. Problem is when I want to use python… Requests is not working for me.

See below

curl -u “USER:PASS” -L -O https://api.bitbucket.org/2.0/repositories/EEEE/AAA/downloads/zip.zip

But how can I mimic this with requests. Can this be done?

import requests
from requests.auth import HTTPBasicAuth

url = "https://bitbucket.org/SYA/Music/downloads/SSR131.zip"
src = r"C:\1\1\SR131.zip"

headers = {'Content-Type': 'application/json'}

respo = requests.get(url, stream=True, verify=False, auth=('##############', '######################'), headers=headers)

if respo.status_code == requests.codes.ok:
    out = open(src, "wb")
    for block in respo.iter_content(1024):
        if not block:
            break

        out.write(block)
    out.close()
else:
    print("Not able to download ZIP url {url}: {status}, {content}".format(url=url, status=respo.status_code, content=respo.content))

Error:

Not able to download ZIP url https…: 401, b’’

More specifically:

Debug output of Requests:

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.bitbucket.org
C:\Users\Imapotatofish\AppData\Local\Continuum\miniconda3\lib\site-packages\urllib3\connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
send: b'GET /2.0/repositories/Imapotatofish/EEE/downloads/zip.zip HTTP/1.1\r\nHost: api.bitbucket.org\r\nUser-Agent: python-requests/2.18.4\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nAuthorization: Basic ABC123REDACTED\r\n\r\n'
  InsecureRequestWarning)    repositories/EEEE/AAA/downloads/zip.zi
DEBUG:urllib3.connectionpool:https://api.bitbucket.org:443 "GET /2.0/repositories/Imapotatofish/EEE/downloads/zip HTTP/1.1" 401 0
reply: 'HTTP/1.1 401 Unauthorized\r\n'
header: Server header: WWW-Authenticate header: Content-Type header: Strict-Transport-Security header: Date header: X-Served-By header: X-Static-Version header: ETag header: X-Render-Time header: Connection header: X-Version header: X-Request-Count header: X-Frame-Options header: Content-Length Not able to download ZIP url https://api.bitbucket.org/2.0/repositories/Imapotatofish/EEE/downloads/zip: 401, b''

@Therewasatime,
I’m not exactly sure what you’re trying to do here. There is a feature of Bitbucket where you can upload/download files that are associated with a repository, but are not version controlled. Those can be accessed with the 2.0 API through: https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/downloads/{filename}

I just successfully tested this out with both curl and python (see below) and an App Password (https://bitbucket.org/account/user/{username}/app-passwords). I gave the App Password Read access to all of my repositories for this to work.

Note: if you have 2FA enabled, your account password won’t work for Basic Auth.

I noticed from your example that you aren’t using the same url for curl and for your python script. I tried using a similar url to the one in your python script and also had problems.

import requests
from requests.auth import HTTPBasicAuth

response = requests.get('https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/downloads/{filename}', auth=HTTPBasicAuth('{username}', '{app_password}'))

with open("filename", 'wb') as f:
    for chunk in response.iter_content(chunk_size=1024): 
        if chunk:
            f.write(chunk)

Now, if you’re looking to download files that are version controlled, you’ll need to use the source API:

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/{username}/{repo_slug}/src/{node}/{path}

Please let me know if you have any additional questions.

ERIC HENRY
Development Team Lead, Bitbucket Cloud

That’s excellent, thank you. So simple auth works. I have an exe I want to keep updating so now I suppose the question is why this works and why this other script of mine does not download. I will post it as separate question