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

问题描述 投票:1回答:2

我有一个用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
}
docker go google-api certificate alpine
2个回答
1
投票

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

Google CA证书就是这个https://pki.google.com/GIAG2.crt

有关证书的更多信息,请访问here

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

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

1
投票

问题不是Go而是Alpine图像造成的。

默认的Alpine图像没有证书,因此应用程序无法调用https地址(此案例为https://accounts.google.com/o/oauth2/token)。

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

apk add --no-cache ca-certificates openssl
© www.soinside.com 2019 - 2024. All rights reserved.