I need to get the file from repository via api from bitbucket using the appPassword.
I generated the appPassword with read and admin for projects and repository and try to use it in my .net application but always have an exeption Forbiden(403).
My code
try
{
var username = _settings.Username;
var appPassword = _settings.AppPassword;
var url = _settings.ConfigFileUrl;
var authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}: {appPassword}"));
using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authToken);
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
}
In userName
I use not my email but my usrname from account settings.
In appPassword
I use generated password from screenshot.
In url
I use url to my final file in repository
In catch section get always the exception - Forbiden 403
What is wrong?
To download a file from a Bitbucket repository using the Bitbucket REST API and an app password in a .NET application, you can use the HttpClient class with basic authentication.
Make sure you:
1. Use the correct API endpoint:
https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/src/{commit_or_branch}/{filepath
}
2. Provide your Bitbucket username and app password using Basic Auth headers.
Here’s a working example in C# that demonstrates how to retrieve the contents of a file from a Bitbucket repository:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
string username = "your-bitbucket-username";
string appPassword = "your-app-password";
string repoOwner = "your-username-or-workspace";
string repoSlug = "your-repository-name";
string branchOrCommit = "main"; // or specific commit hash
string filePath = "path/to/your/file.txt"; // path within the repo
string url = $"https://api.bitbucket.org/2.0/repositories/{repoOwner}/{repoSlug}/src/{branchOrCommit}/{filePath}";
using (HttpClient client = new HttpClient())
{
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{appPassword}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
string fileContent = await response.Content.ReadAsStringAsync();
Console.WriteLine("File content:");
Console.WriteLine(fileContent);
}
else
{
Console.WriteLine($"Failed to get file. Status: {response.StatusCode}");
string error = await response.Content.ReadAsStringAsync();
Console.WriteLine("Error response:");
Console.WriteLine(error);
}
}
}
}
Replace the placeholder values (your-bitbucket-username, your-repository-name, etc.) with your actual data. This script will print the file content if successful or print the error if it fails.
\> Note: If your file is binary (e.g., an image or PDF), use response.Content.ReadAsByteArrayAsync() instead of ReadAsStringAsync() to handle the data correctly.