# Unable to download from bitbucket cloud with Python requests

**URL:** <https://community.developer.atlassian.com/t/unable-to-download-from-bitbucket-cloud-with-python-requests/13423>\
**Category:** Bitbucket Cloud\
**Created:** [January 12, 2018, 4:25pm UTC](https://community.developer.atlassian.com/t/unable-to-download-from-bitbucket-cloud-with-python-requests/13423 "2018-01-12T16:25:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Therewasatime](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@Therewasatime](https://community.developer.atlassian.com/u/Therewasatime)\
**Post date:** [January 12, 2018, 4:25pm UTC](https://community.developer.atlassian.com/t/unable-to-download-from-bitbucket-cloud-with-python-requests/13423/1 "2018-01-12T16:25:37Z")

</div>

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](https://api.bitbucket.org/2.0/repositories/EEEE/AAA/downloads/zip.zip)

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

```auto
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:

```auto
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''

```

---

<div class="post-metadata">

**Author:** ![ehenry](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/ehenry/32/36074_2.png) [@ehenry](https://community.developer.atlassian.com/u/ehenry)\
**Post date:** [January 12, 2018, 7:29pm UTC](https://community.developer.atlassian.com/t/unable-to-download-from-bitbucket-cloud-with-python-requests/13423/2 "2018-01-12T19:29:57Z")

</div>

@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}](https://api.bitbucket.org/2.0/repositories/%7Busername%7D/%7Brepo_slug%7D/downloads/%7Bfilename%7D)

I just successfully tested this out with both curl and python (see below) and an App Password ([Log in with Atlassian account](https://bitbucket.org/account/user/%7Busername%7D/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.

```auto
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}](https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/src/%7Bnode%7D/%7Bpath%7D)

Please let me know if you have any additional questions.

ERIC HENRY  
Development Team Lead, Bitbucket Cloud

---

<div class="post-metadata">

**Author:** ![Therewasatime](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@Therewasatime](https://community.developer.atlassian.com/u/Therewasatime)\
**Post date:** [January 12, 2018, 8:47pm UTC](https://community.developer.atlassian.com/t/unable-to-download-from-bitbucket-cloud-with-python-requests/13423/3 "2018-01-12T20:47:17Z")

</div>

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
