Golang的HTTP基本身份验证

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

Golang HTTP Basic Auth

问题

我是新手使用golang。

我试图使用基本身份验证调用JIRA rest api,但是出现以下错误

&{401 Unauthorized 401 HTTP/2.0 2 0
map[X-Content-Type-Options:[nosniff]
Set-Cookie:[JSESSIONID=E08620; Path=/;
Secure; HttpOnly studio.crowd.tokenkey="";
Domain=.test.atlassian.net; Expires=Thu, 01-Jan-1970 00:00:10
GMT; Path=/; Secure; HttpOnly studio.crowd.tokenkey="";
Domain=.test.atlassian.net; Expires=Thu, 01-Jan-1970 00:00:10
GMT; Path=/; Secure; HttpOnly] Server:[nginx] Date:[Mon, 09 Jan 2017
05:18:29 GMT] Content-Type:[text/html;charset=UTF-8]
X-Arequestid:[648x20759x1] X-Seraph-Loginreason:[AUTHENTICATED_FAILED]
Www-Authenticate:[OAuth
realm="https%3A%2F%2Ftest.atlassian.net"]] {0xc42039b900} -1 []
false false map[] 0xc4200ce0f0 0xc4202b0420}

我的代码是

package main

import (
	"fmt"
	"log"
	"net/http"
)

const (
	USERNAME = ""
	PASSWORD = ""
	URL		 = "https://test.atlassian.net/rest/api/2/issue/SLUB-7194"
)

func main() {
	req, err := http.NewRequest("GET", URL, nil)
    req.SetBasicAuth(USERNAME, PASSWORD)

	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")
	
	cli := &http.Client{}
	resp, err := cli.Do(req)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(resp);
}

我做错了什么吗?

提前感谢。

英文:

I am new in golang.

I am trying to call JIRA rest api with basic auth, but getting following error

&{401 Unauthorized 401 HTTP/2.0 2 0
map[X-Content-Type-Options:[nosniff]
Set-Cookie:[JSESSIONID=E08620; Path=/;
Secure; HttpOnly studio.crowd.tokenkey="";
Domain=.test.atlassian.net; Expires=Thu, 01-Jan-1970 00:00:10
GMT; Path=/; Secure; HttpOnly studio.crowd.tokenkey="";
Domain=.test.atlassian.net; Expires=Thu, 01-Jan-1970 00:00:10
GMT; Path=/; Secure; HttpOnly] Server:[nginx] Date:[Mon, 09 Jan 2017
05:18:29 GMT] Content-Type:[text/html;charset=UTF-8]
X-Arequestid:[648x20759x1] X-Seraph-Loginreason:[AUTHENTICATED_FAILED]
Www-Authenticate:[OAuth
realm="https%3A%2F%2Ftest.atlassian.net"]] {0xc42039b900} -1 []
false false map[] 0xc4200ce0f0 0xc4202b0420}

My code is

package main

import (
	"fmt"
	"log"
	"net/http"
)

const (
	USERNAME = ""
	PASSWORD = ""
	URL		 = "https://test.atlassian.net/rest/api/2/issue/SLUB-7194"
)

func main() {
	req, err := http.NewRequest("GET", URL, nil)
    req.SetBasicAuth(USERNAME, PASSWORD)

	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")
	
	cli := &http.Client{}
	resp, err := cli.Do(req)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(resp);
}

Am I doing something wrong?

Thanks in advance

答案1

得分: 6

Netflix-Skunkworks/go-jira/cli.go中所示,您可能需要先登录,然后请求您的Jira工单。

func (c *Cli) makeRequest(req *http.Request) (resp *http.Response, err error) {
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")

	if source, ok := c.opts["password-source"]; ok && !strings.HasSuffix(req.URL.Path, "/rest/auth/1/session") {
		user, _ := c.opts["user"].(string)
		password := c.GetPass(user)
		if password == "" {
			log.Warning("No password for user %s in %s, please run the 'login' command first", user, source)
		} else {
			req.SetBasicAuth(user, password)
		}
	}
}
英文:

As seen in Netflix-Skunkworks/go-jira/cli.go, you might need to login first, then request your jira ticket.

func (c *Cli) makeRequest(req *http.Request) (resp *http.Response, err error) {
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")

	if source, ok := c.opts["password-source"]; ok && !strings.HasSuffix(req.URL.Path, "/rest/auth/1/session") {
		user, _ := c.opts["user"].(string)
		password := c.GetPass(user)
		if password == "" {
			log.Warning("No password for user %s in %s, please run the 'login' command first", user, source)
		} else {
			req.SetBasicAuth(user, password)
		}
}

huangapple
  • 本文由 发表于 2017年1月9日 13:25:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/41541511.html
匿名

发表评论

匿名网友

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

确定