在c# .net中读取带有私有存储库认证的BitBucket API

本文关键字:认证 BitBucket API 存储 net 读取 | 更新日期: 2023-09-27 18:02:01

我已经尝试了几天让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();
    }
许多谢谢。

在c# .net中读取带有私有存储库认证的BitBucket API

我最近遇到了同样的问题,我找到了两种不同的解决方案。

首先,香草。net与HttpWebRequestHttpWebResponse:
(这是来自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();
}  

或者,如果你想用更少的代码做同样的事情,你可以使用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来获取存储库列表。我不想在我的项目中包含另一个库,只是为了为这一个调用保存几行代码。

另外,您可以使用Bitbucket.Cloud.Net:

  1. 获取令牌,例如使用Flurl。Http:
    var uvalue = "your_username OR consumer_key";// see https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html
    var pvalue = "your_password OR consumer_secret";// see https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html
    var urlAccessToken = "https://bitbucket.org/site/oauth2/" + "access_token";
    var response = await urlAccessToken
                                       .WithBasicAuth(uvalue, pvalue)
                                       .PostUrlEncodedAsync(new
                                       {
                                           grant_type = "client_credentials"
                                       })
                                       .ReceiveJson().ConfigureAwait(false);
    var token = response.access_token;
    // Do something with your authenticated client.

然后,使用检索到的标记,

var oauth = new Bitbucket.Cloud.Net.Common.Authentication.OAuthAuthentication(token);
var bitbucket = new Bitbucket.Cloud.Net.BitbucketCloudClient("https://api.bitbucket.org/", oauth);
//your workspace name is UserFoo
//returns list of Bitbucket.Cloud.Net.Models.v2.Repository
var list = await bitbucket.GetWorkspaceRepositoriesAsync("<your workspace name>").ConfigureAwait(false);

使用WebClient仍然是可能的,你只需要手动创建授权头,就像Christian Specht的回答:

string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("your_username" + ":" + "your_password"));
client.Headers[ HttpRequestHeader.Authorization ] = "Basic " + credentials);