使用 C#.net 中私有存储库的身份验证读取 BitBucket API [英] Reading the BitBucket API with Authentication for a Private Repository in C#.net

查看:23
本文介绍了使用 C#.net 中私有存储库的身份验证读取 BitBucket API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几天让 BitBucket 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)

代码示例如下:

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.

首先,带有 HttpWebRequestHttpWebResponse 的 vanilla .net:
(这来自 Stack Overflow 上的一个答案,但不幸的是我再也找不到链接了)

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();
}  

或者,如果你想用更少的代码做同样的事情,你可以使用 RestSharp:

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;   

<小时>

顺便说一下,我决定为我自己的应用程序使用 HttpWebRequest 解决方案.
我正在编写一个小工具来将我所有的 Bitbucket 存储库(包括私有存储库)克隆到我的本地机器上.因此,我只需一次调用 Bitbucket API 即可获取存储库列表.
我不想在我的项目中包含另一个库,只是为了为这个单一调用节省几行代码.


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.

这篇关于使用 C#.net 中私有存储库的身份验证读取 BitBucket API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆