Reading the BitBucket API with Authentication for a Private Repository in C#.net
I had the same problem recently, and I found two different solutions.
First, vanilla .net with HttpWebRequest
and HttpWebResponse
:
(this came from an answer here at Stack Overflow, but unfortunately I can't find the link anymore)
string url = "https://api.bitbucket.org/1.0/repositories/your_username/your_repo/issues/1";
var request = WebRequest.Create(url) as HttpWebRequest;
string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("your_username" + ":" + "your_password"));
request.Headers.Add("Authorization", "Basic " + credentials);
using (var response = request.GetResponse() as HttpWebResponse)
{
var reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadToEnd();
}
Or, if you want to do the same with less code, you can use RestSharp:
var client = new RestClient("https://api.bitbucket.org/1.0/");
client.Authenticator =
new HttpBasicAuthenticator("your_username", "your_password");
var request = new RestRequest("repositories/your_username/your_repo/issues/1");
RestResponse response = client.Execute(request);
string json = response.Content;
By the way, I decided to use the HttpWebRequest
solution for my own application.
I'm writing a small tool to clone all my Bitbucket repositories (including the private ones) to my local machine. So I just make one single call to the Bitbucket API to get the list of repositories.
And I didn't want to include another library in my project just to save a few lines of code for this one single call.