阅读与认证到位桶API在C#.NET中的私人信息库 [英] Reading the BitBucket API with Authentication for a Private Repository in C#.net
问题描述
我一直试图几天拿到到位桶API来为我工作,但来了嘎然而止的时候,它会得到它的私人存储库的认证工作(与问题设置为私有当它们设置为公开,且无需验证这一切工作正常)
I've been trying for a few days to get the BitBucket API to work for me, but have come to a grinding halt when it comes to getting it to work for a private repository with authentication (with the issues set as private, when they're set to public and no authentication is needed it all works fine)
code示例如下:
static void Main(string[] args)
{
WebProxy prox = new WebProxy("ProxyGoesHere");
prox.Credentials = CredentialCache.DefaultNetworkCredentials;
var address = "repositories/UserFoo/SlugBar/issues/1";
var repCred = new CredentialCache();
repCred.Add(new Uri("https://api.bitbucket.org/"), "Basic", new NetworkCredential("UserFoo", "PassBar"));
WebClient client = new WebClient();
client.Credentials = repCred;
client.Proxy = prox;
client.BaseAddress = "https://api.bitbucket.org/1.0/";
client.UseDefaultCredentials = false;
client.QueryString.Add("format", "xml");
Console.WriteLine(client.DownloadString(address));
Console.ReadLine();
}
非常感谢。
推荐答案
最近,我有同样的问题,我发现了两个不同的解决方案。
I had the same problem recently, and I found two different solutions.
首先,香草.NET与的HttpWebRequest
和 HttpWebResponse
:结果
的(这从这里走过的堆栈溢出的答案,可惜我不能再找到链接)的
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();
}
或者,如果你想要做同样的用量少code,可以使用 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;
的顺便说一句,我决定使用的的HttpWebRequest
的解决方案,为我自己的应用程序。结果
我正在写一个小工具,克隆我所有的到位桶的仓库(包括私营的)到我的本地机器上。所以,我只是做有一个单到位桶API调用来获取存储库列表。结果
我不想在自己的项目只是为了节省code的几行这一个单一的呼叫另一个库。的
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.
这篇关于阅读与认证到位桶API在C#.NET中的私人信息库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!