英文:
Get "https://zzzztower.zzzz.com/api/v2/hosts/": x509: certificate relies on legacy Common Name field, use SANs instead
问题
我正在尝试使用这里中定义的API从Ansible Tower获取主机列表。
我正在使用Ansible Tower的URL -> https://zzzztower.zzzz.com/api/v2/hosts/
和Bearer令牌 -> aaaaaaaaaaaaaaaaaaaaaaaaaaa
来访问API。
当我使用Postman访问API时,我得到了正确的响应,但是当我使用Golang代码时,我遇到了错误
> Get "https://ansibletower.micron.com/api/v2/hosts/": x509: 证书依赖于传统的Common Name字段,应改用SANs
这是我的代码:
package globals
import (
"fmt"
"io/ioutil"
"net/http"
)
// var AUTH_TOKEN string = os.Getenv("AUTH_TOKEN")
func GetAnsibleHosts() (string, error) {
url := "https://zzzztower.zzzz.com/api/v2/hosts/"
method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return "", fmt.Errorf("Error creating request: %v", err)
}
bearerToken := "aaaaaaaaaaaaaaaaaaaaaaaaaaa"
// Add the Bearer Auth token to the request
req.Header.Add("Authorization", "Bearer "+bearerToken)
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return "", err
}
// fmt.Println(string(body))
return string(body), err
}
我尝试在Google上找到错误,但没有找到太多帮助。
一些文章提到使用GODEBUG=x509ignoreCN=0
,但没有起作用。
我真的很感谢你的帮助。
英文:
I am trying to get the list of hosts from ansible tower using the API defined in here
I am using the ansible tower URL -> https://zzzztower.zzzz.com/api/v2/hosts/
and the bearer token -> aaaaaaaaaaaaaaaaaaaaaaaaaaa
to hit the API.
When I use postman to hit the API I am getting a proper response
but when I use golang code, I am getting error
> Get "https://ansibletower.micron.com/api/v2/hosts/": x509: certificate relies on legacy Common Name field, use SANs instead
Here is my code:
package globals
import (
"fmt"
"io/ioutil"
"net/http"
)
// var AUTH_TOKEN string = os.Getenv("AUTH_TOKEN")
func GetAnsibleHosts() (string, error) {
url := "https://zzzztower.zzzz.com/api/v2/hosts/"
method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return "", fmt.Errorf("Error creating request: %v", err)
}
bearerToken := "aaaaaaaaaaaaaaaaaaaaaaaaaaa"
// Add the Bearer Auth token to the request
req.Header.Add("Authorization", "Bearer "+bearerToken)
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return "", err
}
// fmt.Println(string(body))
return string(body), err
}
I tried finding the error on google but i didn't find much help.
Few articles mentioned to use GODEBUG=x509ignoreCN=0
but it didn't worked.
I would really appreciate you help.
答案1
得分: 0
要跳过此检查,我们可以使用InsecureSkipVerify
并将其设置为true。
类似这样:
// 创建一个新的传输对象,并将InsecureSkipVerify设置为true
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// 使用该传输对象创建一个新的客户端
client := &http.Client{Transport: transport}
然后可以使用这个client
来获取响应。
PS:这对我有效,但如果有更好的方法,请其他人提出建议。
英文:
To skip this check, we can use InsecureSkipVerify
and set it to true.
Something like this:
// Create a new transport with InsecureSkipVerify set to true
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Create a new client using the transport
client := &http.Client{Transport: transport}
And then this client
can be used to get the response.
PS: This worked for me but others pls suggest a better approach if any.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论