获取GitLab仓库列表:显示“401未经授权”。

huangapple go评论88阅读模式
英文:

Fetching gitlab repo list : says "401 Unauthorized"

问题

我正在尝试使用OAuth令牌从GitLab获取仓库列表。

我的代码大致如下... ("github.com/xanzy/go-gitlab")

repositories := []string{}
client, _ := gitlab.NewClient(gitRepoRequest.Token, gitlab.WithBaseURL("https://gitlab.com/api/v4"))
fmt.Println("client...", client.ContainerRegistry)

projects, _, projectListErr := client.Projects.ListProjects(&gitlab.ListProjectsOptions{})
for _, project := range projects {
fmt.Println("ID===", project.ID)
fmt.Println("NAME===", project.Name)
}

if projectListErr != nil {
// 返回错误
}

我无法获取项目列表... "projectListErr" 的错误信息是...
GET https://gitlab.com/api/v4/projects: 401 {message: 401 Unauthorized}

我对令牌值很有信心,因为我使用相同的令牌获取了仓库的所有分支列表,那段代码如下... ("github.com/go-git/go-git/v5")

rem := git.NewRemote(gitMemory.NewStorage(), &gitConfig.RemoteConfig{
Name: "origin",
URLs: []string{gitBranchesRequest.Repository},
})

refs, listErr := rem.List(&git.ListOptions{
Auth: &gitHttp.BasicAuth{Username: gitUserName, Password: gitBranchesRequest.Token},
})

这是否意味着我使用的库 "github.com/xanzy/go-gitlab" 存在问题?

英文:

I am trying to get repo list from gitlab using OAuth token.

My code looks something like this ... ("github.com/xanzy/go-gitlab")

    repositories := []string{}
	client, _ := gitlab.NewClient(gitRepoRequest.Token, gitlab.WithBaseURL("https://gitlab.com/api/v4"))
	fmt.Println("client...", client.ContainerRegistry)

	projects, _, projectListErr := client.Projects.ListProjects(&gitlab.ListProjectsOptions{})
	for _, project := range projects {
		fmt.Println("ID===", project.ID)
		fmt.Println("NAME===", project.Name)
	}

	if projectListErr != nil {
		// return err
	}

I am not able to get the project list.. the "projectListErr" says ...
GET https://gitlab.com/api/v4/projects: 401 {message: 401 Unauthorized}

I am confident about the token value because I am getting list of all branches for a repo using the same token, that code looks like ... ("github.com/go-git/go-git/v5")

rem := git.NewRemote(gitMemory.NewStorage(), &gitConfig.RemoteConfig{
	Name: "origin",
	URLs: []string{gitBranchesRequest.Repository},
})

refs, listErr := rem.List(&git.ListOptions{
	Auth: &gitHttp.BasicAuth{Username: gitUserName, Password: gitBranchesRequest.Token},
})

Does that mean there is an issue with the library that I am using ? github.com/xanzy/go-gitlab

答案1

得分: 2

这取决于你使用的令牌类型。

例如,一个项目访问令牌可能会给你访问某个项目的所有分支的权限。

但是对于使用/projects API,401表示身份验证信息无效或缺失。

所以请确保使用与用户关联的个人访问令牌(PAT),而不是项目访问令牌。

OP Keval Bhogayata在评论中补充道:

我找到了问题。

我使用的库("xanzy/go-gitlab")为不同的令牌创建函数提供了不同的客户端创建方式。

我一直在使用支持个人访问令牌的函数,而应该使用"NewOAuthClient"!

// NewOAuthClient返回一个新的GitLab API客户端。要使用需要身份验证的API方法,请提供有效的OAuth令牌。
func NewOAuthClient(token string, options ...ClientOptionFunc) (*Client, error)
英文:

It depends on the type of token you are using.

For instance, a project access token might very well give you access to the list of all branches for a repository (for that project).

But for using the /projects API, 401 means the authentication information is not valid or is missing.

So make sure to use a PAT (Personal Access Token), linked to a user, not a project.


The OP Keval Bhogayata adds in the comments:

> I have found the issue.
>
> The library I am using ("xanzy/go-gitlab"), has different client creation functions for different tokens.
>
> I have been using the function that supports personal access token. Instead I was supposed to use "NewOAuthClient" !
>
>go
> // NewOAuthClient returns a new GitLab API client. To use API methods which
> // require authentication, provide a valid oauth token.
> func NewOAuthClient(token string, options ...ClientOptionFunc) (*Client, error)
>

huangapple
  • 本文由 发表于 2021年12月30日 21:39:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/70532432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定