I have a powershell script which grabs a file from one of our repositories via the REST 1.0 API but I see that's being depreciated and retired soon(ish).
I'm trying to update my code to grab the file using the 2.0 REST API but having a lot of trouble.
Before trying to write code I'm simply trying to get a curl request to work. I'm using the following:
curl --user myuser@mydomain.com:redacted "https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/downloads/Scripts/Environment Setup/test.txt"
Enter host password for user 'myusername@mydomain.com': 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/"}}
Why am I getting resource not found? Is download the right API call to download a file or should I be using something else?
Thanks,
Brad
Actually I just realized that "downloads" isn't what I want. Instead I needed to use:
curl --user myuser@mydomain.com:redacted "https://api.bitbucket.org/2.0/repositories/mycompany/myrepo/src/master/path/to/file.txt"
That works with curl but not with powershell's Invoke-RestAPI. I get a forbidden:
$filepath = "myrepo/src/master/path/to/file"
$uri = "https://api.bitbucket.org/2.0/repositories/MyCompany/$($filepath)"
# Get the files from bitbucket (GIT)
Invoke-RestMethod -Credential $cred -Uri $uri -Proxy $proxyUri -OutFile $destination
----
Exception : System.Net.WebException: The remote server returned an error: (403) Forbidden.
Seems the new bit bucket API expects credentials to be send in the request - and Invoke-RestMethod sends them after its prompted to causing an immediate 403 forbidden. The 1.0 api doesn't have this issue for whatever reason.
The (ugly) solution is:
$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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Brad,
Thanks for writing out the steps. I'm also trying to use a PowerShell solution to download files via the API.
Does this approach still work for you or have you had to change it?
I've tried both the URI you listed (adapted for my repository) as well as the one in the reference documentation:
$uri = https://<hostname>/rest/api/1.0/users/<myName>/<myRepo>/path/to/file
But I am getting either error 401, not authorized, or only a json output description of the directory rather than the files.
I generated an admin level pass token and set my repo to public. I'm using the same user name I use in the browser to log in and copied the credential generation you used.
Were there any other steps that might have been necessary for you? For example, how is the proxyUri parameter determined? Also, is there a way to verify the correct uri definitively?
Thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is only working for master or ci branch, unable to read a file from feature branch
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I had the same issue, to read from a particular file, instead of adding the branch name on the url, send a post request for the branch using -F branch=<your-branch>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.