Py学习  »  Git

获取gitlab回购清单:显示“401未经授权”

Keval Bhogayata • 2 年前 • 523 次点击  

我正在尝试使用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 {
        // return err
    }

我无法得到项目清单。。“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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133232
 
523 次点击  
文章 [ 1 ]  |  最新文章 2 年前
VonC
Reply   •   1 楼
VonC    2 年前

这取决于天气 type of token you are using .

例如,一个 project access token 很可能会允许您访问存储库(该项目)的所有分支的列表。

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

所以一定要使用 PAT (Personal Access Token) ,链接到用户,而不是项目。


这个 OP Keval Bhogayata 加上 the comments :

我发现了问题所在。

我正在使用的图书馆(” xanzy/go-gitlab “”),具有针对不同令牌的不同客户端创建功能。

我一直在使用支持个人访问令牌的功能。相反,我应该用“ NewOAuthClient " !

// 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)