为了账号安全,请及时绑定邮箱和手机立即绑定

无法从 Docker 容器内的 Google API 交换 AccessToken

无法从 Docker 容器内的 Google API 交换 AccessToken

Go
慕雪6442864 2023-04-04 15:32:44
我有一个用 Go 编写的网络应用程序,使用 oauth2(包golang.org/x/oauth2)通过 Google 登录用户(按照本教程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}
查看完整描述

2 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

问题不是由 Go 引起的,而是 Alpine 图像引起的。

默认的 Alpine 图像没有证书,因此应用程序无法调用 https 地址(

要解决此问题,请安装 2 个软件包opensslca-certificates. Dockerfile 中的示例:

apk add --no-cache ca-certificates openssl


查看完整回答
反对 回复 2023-04-04
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

您需要将 Google Issuing CA 证书添加到 docker 映像的受信任证书存储中。

然后在 Dockerfile 中,你需要做这样的事情

cp GIAG2.crt /usr/local/share/ca-certificates/GIAG2.crt
update-ca-certificates


查看完整回答
反对 回复 2023-04-04
  • 2 回答
  • 0 关注
  • 98 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信