gpt4 book ai didi

C# 可以写入但不能读取存储库的 BitBucket API

转载 作者:太空宇宙 更新时间:2023-11-03 14:45:05 24 4
gpt4 key购买 nike

我正在尝试使用 C# 访问 BitBucket API。我可以执行某些操作,但不能执行其他操作。值得注意的是,写入存储库有效,但读取它们则无效。

using System.Net;
using System.Collections.Specialized;

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // TLS v1.2 only
var client = new WebClient()
{
Credentials = new NetworkCredential("user", "app_password"),
BaseAddress = "https://api.bitbucket.org",
};

client.DownloadString(
"/2.0/repositories/friendly_private_account/repo"); // 403 Forbidden
client.DownloadString(
"/2.0/repositories/friendly_private_account/repo/src"); // 403 Forbidden
client.UploadValues(
"/2.0/repositories/friendly_private_account/repo/src",
new NameValueCollection() {
{ "/bb.txt", "here is\nsome content\n" },
{ "message", "Commit from API, called with C# WebClient" },
}); // Creates a commit! What!?

这有点奇怪,因为如果您在创建应用程序密码时启用了write 权限,您会自动获得read 权限。

DownloadString() 也不是问题。 App Password如果有webhook权限,就可以读取Web Hooks。

client.DownloadString(
"/2.0/repositories/friendly_private_account/repo/hooks");
// {"pagelen": 10, "values": [{ … }]}

有趣的是,curl 使用相同的凭据没有任何问题。

$ curl --user "${user}:${app_password}" \
--url "https://api.bitbucket.org/2.0/repositories/friendly_private_account/repo"
# {"scm": "git", "website": "", "has_wiki": false, … }

使用 --verbose 运行 curl 实际上会返回描述您的凭据具有哪些权限的 header 需要哪些权限。在上面的例子中,它需要repository,而我有repository:write。它并没有说我有 repository:read,但请求仍然成功。

最佳答案

听起来 WebClient 只发送 Authorization header when the server responds with a 401 Unauthorized .

也许 BitBucket 在一些未经身份验证的端点上以 401 响应,激发 WebClient 重新发送带有身份验证的请求;但对其他人返回 403,立即结束请求。

显式添加 Authorization header 可以解决问题,尽管有点难看。

using System.Net;
using System.Collections.Specialized;

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // TLS v1.2 only
var client = new WebClient()
{
// Credentials = new NetworkCredential("user", "app_password"), // Take this line out
BaseAddress = "https://api.bitbucket.org",
};

client.Headers[HttpRequestHeader.Authorization] =
"Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("user:app_password"));

client.DownloadString(
"/2.0/repositories/friendly_private_account/repo"); // Now it works

关于C# 可以写入但不能读取存储库的 BitBucket API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54697989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com