andygrunwald / go-jira

Go client library for Atlassian Jira
https://pkg.go.dev/github.com/andygrunwald/go-jira?tab=doc
MIT License
1.47k stars 469 forks source link

Can't connect to Jira account (401 unauthorized) #613

Closed icen1 closed 1 year ago

icen1 commented 1 year ago

What happened?

I tried to connect to my jira account like in the example but it failed with the following error &{%!e(string=401 Unauthorized: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><status><status-code>401</status-code><message>Client must be authenticated to access this resource.</message></status>: request failed. Please analyze the request body for more details. Status code: 401) %!e(*errors.errorString=&{request failed. Please analyze the request body for more details. Status code: 401})}exit status 0xffffffff I checked all the issued with unauthorized and 401 that i could find and i didn't get to anything, the following is my code:

package main

import (
    "context"
    "fmt"
    "os"

    jira "github.com/andygrunwald/go-jira/v2/cloud"
)

func main() {
    jiraURL := "https://go-jira-opensource.atlassian.net/"

    // Jira docs: https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/
    // Create a new API token: https://id.atlassian.com/manage-profile/security/api-tokens
    // tr := &http.Transport{
    // TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    // }
    // client1 := &http.Client{Transport: tr}
    tp := jira.BasicAuthTransport{
        Username: "<correct email>",
        APIToken: "<api token>", // i know those are correct as my api token gets a connection everytime i use it.
    }
    client, err := jira.NewClient(jiraURL, tp.Client())
    if err != nil {
        fmt.Printf("Error at making a new client: %e ", err)
        os.Exit(-1)

    }

    u, _, err := client.User.GetCurrentUser(context.Background())
    if err != nil {
        fmt.Printf("Error at getting user: %e", err)
        os.Exit(-1)
    }

    fmt.Printf("Email: %v\n", u.EmailAddress)
    fmt.Println("Success!")
}

What did you expect to happen?

I expected it to connect and print the email address used

How can we reproduce it (as minimally and precisely as possible)?

If you use my above code and input your email and token you should be able to face the same issue

Anything else we need to know?

N/A

Your Environment

Include as many relevant details about the environment you experienced the problem in

andygrunwald commented 1 year ago

Hey @icen1, you marked this ticket as closed. Did you fix the issue? If yes, what was the cause? This could help other community members with a similar problem.

icen1 commented 1 year ago

Yes ofcourse. It was just a dumb mistake on my part. I left the website as the one in the example and didn't change it to my Jira website.

andygrunwald commented 1 year ago

Thx.