我已经尝试了几天让 BitBucket API 为我工作,但是当涉及到让它为具有身份验证的私有存储库工作时(将问题设置为私有,当它们设置为公开,无需身份验证,一切正常)
代码示例如下:
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();
}
非常感谢。
我最近遇到了同样的问题,我找到了两种不同的解决方案。
首先,香草 .net 与HttpWebRequest
and HttpWebResponse
:
(这来自 Stack Overflow 的答案,但不幸的是我找不到链接了)
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();
}
或者,如果您想用更少的代码执行相同的操作,您可以使用休息锐利 http://restsharp.org/:
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
我自己的应用程序的解决方案。
我正在编写一个小工具来将我的所有 Bitbucket 存储库(包括私有存储库)克隆到我的本地计算机。所以我只是做one对 Bitbucket API 的一次调用即可获取存储库列表。
我不想在我的项目中包含另一个库只是为了为这个单一调用节省几行代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)