我有一個用 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 個軟件包openssl
和ca-certificates
.?Dockerfile 中的示例:
apk?add?--no-cache?ca-certificates?openssl

哈士奇WWW
TA貢獻1799條經驗 獲得超6個贊
您需要將 Google Issuing CA 證書添加到 docker 映像的受信任證書存儲中。
然后在 Dockerfile 中,你需要做這樣的事情
cp?GIAG2.crt?/usr/local/share/ca-certificates/GIAG2.crt update-ca-certificates
- 2 回答
- 0 關注
- 171 瀏覽
添加回答
舉報
0/150
提交
取消