xanzy / go-gitlab

GitLab Go SDK
Apache License 2.0
2.42k stars 956 forks source link

ListPipelineJobs 401 Unauthorized #348

Closed deimosfr closed 6 years ago

deimosfr commented 6 years ago

Hi,

I'm trying to deploy a pipeline job, for that, I'm using this code:

    git := gitlab.NewClient(nil, viper.GetString("gitlab_token"))

    // Add forms to pipeline trigger
    customForms := make(map[string]string)
    customForms["client_id"] = clientId

    // Generate pipeline trigger
    opt := &gitlab.RunPipelineTriggerOptions{
        Token:      gitlab.String(viper.GetString("gitlab_token")),
        Variables:  customForms,
        Ref:        gitlab.String("deployer"),
    }

    project, _, err := git.PipelineTriggers.RunPipelineTrigger(
        viper.GetString("gitlab_project_id"),
        opt)
    if err != nil {
        log.Error("Wasn't able to create the gitlab pipeline:")
        log.Fatal(err)
        os.Exit(1)
    }

       // Get job ID
    jobs, _, err := git.Jobs.ListPipelineJobs(
        viper.GetString("gitlab_project_id"),
        project.ID, &gitlab.ListJobsOptions{})
    if err != nil {
        log.Error("Wasn't able to list jobs from gitlab pipeline:")
        log.Fatal(err)
        log.Fatal(jobs)
        os.Exit(1)
    }

The error message I got is:

time="2018-02-21T09:27:05+01:00" level=fatal msg="GET https://gitlab.com/api/v4/projects/xxx/pipelines/yyy/jobs: 401 {message: 401 Unauthorized}"

The pipeline creation is ok, but I'm not able to get the job because of the authorization. I tested with curl commands, and it works, I don't see what's wrong. Any help would be appreciated.

Thanks

zaquestion commented 6 years ago

@deimosfr I've successfully been using that endpoint for a new feature in one of my projects. If it helps here's the code https://github.com/zaquestion/lab/blob/ci/internal/gitlab/gitlab.go#L354

Would suggest looking at the returned response struct (second return arg) and making sure the request matches what you'd expect.

deimosfr commented 6 years ago

Thanks for the example @zaquestion, I'll take a look and keep you updated.

deimosfr commented 6 years ago

I looked at the answer, the target URL is good (project id and pipeline id is correct). I see the header set with token. I see every required stuffs that are requested when doing it with curl. I looked at your code as well and we are doing the same thing. However you are listing pipelines before (ListProjectPipelines). Is it mandatory to perform this action before ?

zaquestion commented 6 years ago

@deimosfr I don't believe so, I'm only doing that because I need to search for a specific pipeline

Lorac commented 6 years ago

@deimosfr I tried a few things. One way I was able to replicate by having an empty PRIVATE-TOKEN so the header is there but no value.

Maybe viper.GetString("gitlab_token") returns an empty string? Try passing a hardcoded private token in there and test it out.

Most of the time the 401 is related to the private-token.

I was able to replicate the same result with https://www.getpostman.com/.

When I was passing my private-token I had no trouble to get a response from the API

deimosfr commented 6 years ago

I've found the issue. I though pipeline token was the same as the private token and I was using the private for everything. Thanks for you help :)