登录
首页 >  Golang >  Go问答

尝试访问 GitLab 存储库列表时出现“401 未经授权”错误

来源:stackoverflow

时间:2024-03-28 13:54:27 463浏览 收藏

今天golang学习网给大家带来了《尝试访问 GitLab 存储库列表时出现“401 未经授权”错误》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

我正在尝试使用 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 {消息: 401 未经授权}

我对令牌值很有信心,因为我正在获取使用相同令牌的存储库的所有分支的列表,该代码看起来像......(“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


正确答案


这取决于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)

今天关于《尝试访问 GitLab 存储库列表时出现“401 未经授权”错误》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>