使用GitLab专用令牌调用GitLab端点时发生未经授权的错误

使用GitLab专用令牌调用GitLab端点时发生未经授权的错误,gitlab,dotnet-httpclient,unauthorized,Gitlab,Dotnet Httpclient,Unauthorized,我正试图通过HttpClient访问GitLab API端点。首先,我使用 https://gitlab.example.com/oauth/token 然后,使用这个访问令牌,我试图访问下面有端点的标记或项目 https://gitlab.example.com/api/v4/projects/1/repository/tags/ https://gitlab.example.com/api/v4/projects/ 不使用访问令牌,我可以访问公共信息,但使用令牌时会出现未经授权的错误。

我正试图通过HttpClient访问GitLab API端点。首先,我使用

https://gitlab.example.com/oauth/token
然后,使用这个访问令牌,我试图访问下面有端点的标记或项目

https://gitlab.example.com/api/v4/projects/1/repository/tags/

https://gitlab.example.com/api/v4/projects/
不使用访问令牌,我可以访问公共信息,但使用令牌时会出现未经授权的错误。我甚至在url中尝试了私有令牌,它也给出了同样的错误

https://gitlab.example.com/api/v4/projects/1/repository/tags?private_token=xxxxxxxxxxxxxxxxxxxxxxx
我的代码在下面

private async Task<HttpResponseMessage> GetHttpResponse(string url)
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
                                                | SecurityProtocolType.Tls
                                                | SecurityProtocolType.Tls11
                                                | SecurityProtocolType.Tls12;

    var request = new HttpRequestMessage(HttpMethod.Get, url);
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeJson));
    request.Headers.Add("Private-Token", accessToken);

    return await Client.SendAsync(request).ConfigureAwait(false);
}
专用异步任务GetHttpResponse(字符串url)
{
ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3
|SecurityProtocolType.Tls
|SecurityProtocolType.Tls11
|SecurityProtocolType.Tls12;
var request=newhttprequestmessage(HttpMethod.Get,url);
request.Headers.Accept.Add(新的MediaTypeWithQualityHeaderValue(MediaTypeJson));
request.Headers.Add(“私有令牌”,accessToken);
返回wait Client.sendaync(request).configurewait(false);
}

我不知道出了什么问题以及如何修复它

您也必须发送oauth头文件

curl——标题“授权:承载”https://gitlab.example.com/api/v4/projects


试试看

这样使用将为您提供组和子组项目的项目

curl --silent --header "Private-Token: YOUR_GITLAB_TOKEN" https://gitlab.example.com/api/v4/groups/<group_ID>/projects?include_subgroups=true
curl--silent--header“私有令牌:您的\u GITLAB\u令牌”https://gitlab.example.com/api/v4/groups//projects?include_subgroups=true
或者,如果您只需要特定组中的项目

curl --silent --header "Private-Token: YOUR_GITLAB_TOKEN" https://gitlab.example.com/api/v4/groups/<group_ID>/projects?per_page=100
curl--silent--header“私有令牌:您的\u GITLAB\u令牌”https://gitlab.example.com/api/v4/groups//projects?per_page=100

感谢您将授权而非专用令牌添加到为我工作的头文件
request.Headers.Add(“authorization”、$“Bearer{accessToken}”)