Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

go - How to solve x509: certificate has expired or is not yet valid when doing http requests

I have a web server that gets it's certs from golang.org/x/crypto/acme/autocert

I run it on a VM.

Relevante code:

cache := autocert.DirCache("cert")

certManager := autocert.Manager{
    Prompt:     autocert.AcceptTOS,
    HostPolicy: autocert.HostWhitelist(common.ConfHost.GetString(), "www."+common.ConfHost.GetString()),
    Email:      common.ConfEmail.GetString(),
    Cache:      cache,
}

This is what I'm using for HTTP Requests: resp, err := http.DefaultClient.Get(url)

When I try to fetch from a specific API, I get this error: x509: certificate has expired or is not yet valid current time 2020-12-28T17:29:37Z is after 2020-12-26T08:22:28Z

But it only happens to that specific API. I've tried a few others and they all worked.

The code was working until the 26th of December, when the certificate expired.

On the other hand, I wrote a main.go to test without all the certificates that I have to use on my webserver just to test. When I run the code on my local machine I get the response from the API without any issues.

This is the main.go file: https://play.golang.org/p/-EB9DIjJYQO

How can I fix this issue?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

the current status of the package you are using

This package is a work in progress and makes no API stability promises.

it's hard to tell why that API doesn't work like others, could be something in your code or the package it-self.

you may try certmagic package and test your code against it, it's fully matured and in production already.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...