Getting a file from BitBucket Rest API v2.0

12,088

For posterities sake, you don't want to use the following to download an individual file from bitbucket:

https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/downloads/path/to/your/file.txt

("Downloads" is to download entire repo files like a .zip file)

Instead you want to do:

curl --user [email protected]:password "https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/src/master/path/to/file.txt"

If you're trying to use Invoke-RestRequest (in powershell) note there are some extra steps. With the old 1.0 API you could do:

$cred = Get-Credential

$uri = "https://api.bitbucket.org/1.0/repositories/MyCompany/$($filepath)"

# Get the files from bitbucket (GIT)
Invoke-RestMethod -Credential $cred -Uri $uri -Proxy $proxyUri -OutFile $destination 

With the new 2.0 API that no longer works. Powershell's Invoke-RestMethod waits for a 401 response before sending the credentials, and the new 2.0 bitbucket api never provides one, so credentials never get sent causing a 403 forbidden.

To work around that you have to use the following ugly hack to force Invoke-RestMethod to send the credentials immediately in an Authorization header:

$cred = Get-Credential


$uri = "https://api.bitbucket.org/2.0/repositories/MyCompany/$($filepath)"
$username = ($cred.GetNetworkCredential()).username
$password = ($cred.GetNetworkCredential()).password
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

# Get the files from bitbucket (GIT)
Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Uri $uri -Proxy $proxyUri -OutFile $destination 

Hopefully that helps someone else out in the future!

Thanks @Jim Redmond for the help.

Share:
12,088

Related videos on Youtube

Brad
Author by

Brad

Updated on June 04, 2022

Comments

  • Brad
    Brad almost 2 years

    I have a script which grabs a file from GIT using the bitbucket REST API (1.0) however it has recently stopped working. I'm theorizing this may be due to the v1 REST API being depreciated but I'm not sure.

    Anyway I am trying to retrieve the file using the new 2.0 REST API but I can't seem to get the syntax right as the request continually fails.

    I'm starting out with curl since its easiest to test. This is what I'm trying:

    curl -u [email protected] "https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/downloads/Scripts/Environment Setup/test.txt"
    
    Enter host password for user '[email protected]': redacted
    
    {"type": "error", "error": {"message": "Resource not found", "detail": "There is no API hosted at this URL.\n\nFor information about our API's, please refer to the documentation at: https://developer.atlassian.com/bitbucket/api/2/reference/"}}
    

    Here is the reference documentation I am using: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/downloads/%7Bfilename%7D

    Maybe I am using the wrong function? I'm not sure.

    • Jim Redmond
      Jim Redmond over 5 years
      What happens if you replace the space in that URL with a %20?
    • Brad
      Brad over 5 years
      That doesn't make a difference (same exact message)
    • Brad
      Brad over 5 years
      If I do this: curl --user [email protected]:redacted api.bitbucket.org/2.0/repositories/MyCompany/myrepo/refs/… I get a list of branches. So I know my username is right, my password is right, I can hit the api.bitbucket.com/2.0 URL so I'm not sure if the issue is with the "downloads" feature. Maybe I am using it wrong?
  • Farruh Habibullaev
    Farruh Habibullaev about 4 years
    Excellent. Thank you.