我有一个用 Go 编写的网络应用程序,使用 oauth2 (包golang.org/x/oauth2
)通过 Google 登录用户(按照本教程操作https://developers.google.com/identity/sign-in/web/server-side-flow https://developers.google.com/identity/sign-in/web/server-side-flow).
当我在本地测试应用程序时,它工作正常,但是当我部署应用程序并在 Docker 容器内运行时(基于alpine:latest
,运行二进制文件),出现错误:Post https://accounts.google.com/o/oauth2/token: x509: certificate signed by unknown authority
这是我交换 accessToken 的代码:
ctx = context.Background()
config := &oauth2.Config{
ClientID: config.GoogleClientId,
ClientSecret: config.GoogleClientSecret,
RedirectURL: config.GoogleLoginRedirectUrl,
Endpoint: google.Endpoint,
Scopes: []string{"email", "profile"},
}
accessToken, err := config.Exchange(ctx, req.Code)
if err != nil {
log.Println(err.Error()) // Error here
}
该问题不是 Go 造成的,而是 Alpine 镜像造成的。
默认的 Alpine 镜像没有证书,因此应用程序无法调用 https 地址(本例为https://accounts.google.com/o/oauth2/token https://accounts.google.com/o/oauth2/token).
要解决此问题,请安装 2 个软件包openssl
and ca-certificates
。 Dockerfile 中的示例:
apk add --no-cache ca-certificates openssl
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)