遷移至 Go Admin SDK 4 版

從 4.0.0 版開始,Go 的 Firebase Admin SDK 已選擇加入 Go 模組。此外,錯誤處理和語意也有重大變更。

安裝變更

為符合模組最佳做法,SDK 的主要版本已附加至套件名稱。這項變更會導致下列套件名稱更新:

  • firebase.google.com/go → firebase.google.com/go/v4
  • firebase.google.com/go/auth → firebase.google.com/go/v4/auth
  • firebase.google.com/go/db → firebase.google.com/go/v4/db
  • firebase.google.com/go/iid → firebase.google.com/go/v4/iid
  • firebase.google.com/go/messaging → firebase.google.com/go/v4/messaging

已使用模組的開發人員

使用含版本的套件名稱安裝最新版 SDK。

# Install the latest version:
go install firebase.google.com/go/v4@latest

# Or install a specific version:
go install firebase.google.com/go/v4@4.18.0

匯入 SDK 時,來源程式碼中必須使用相同的版本化套件名稱。

package helloworld

import (
        "firebase.google.com/go/v4"
        "firebase.google.com/go/v4/auth"
        "firebase.google.com/go/v4/messaging"
)

如要安裝舊版,請使用舊版 (未指定版本) 的套件名稱,並明確指定版本限定符。

# Notice the @v3 suffix.
# This instructs Go tools to fetch the latest v3.x release of the SDK.
go get firebase.google.com/go@v3

目前未使用模組的開發人員

尚未選擇模組的開發人員,可以繼續使用未指定版本的套件名稱安裝 SDK。

go get firebase.google.com/go

但請注意,這會擷取最新版 SDK (第 4 版或更新版本),其中包含其他重大 API 變更。

一般錯誤處理變更

v4 版 SDK 推出新的 errorutils 套件,提供處理平台層級錯誤狀況的函式。如果錯誤是由後端服務錯誤所致,您可以呼叫新函式 errorutils.HTTPResponse(),存取原始錯誤回應。您可以搭配 SDK 中任何 API 傳回的錯誤,使用這個套件中的函式。

Authentication API 變更

  • 新增錯誤處理函式,可搭配 VerifyIDToken()VerifySessionCookie() API 使用:
    • IsIDTokenInvalid()
    • IsIDTokenExpired()
    • IsSessionCookieInvalid()
    • IsSessionCookieExpired()
    • IsCertificateFetchFailed()
  • 已淘汰:
    • IsProjectNotFound()
    • IsUnknown()
    • IsInsufficientPermission()
    • IsInvalidEmail()

FCM API 變更

  • 已將型別「messaging.WebpushFCMOptions」重新命名為「messaging.WebpushFcmOptions」。
  • 已新增:
    • IsThirdPartyAuthError()
    • IsQuotaExceeded()
    • IsSenderIDMismatch()
    • IsUnregistered()
    • IsUnavailable()
  • 已淘汰:
    • IsInvalidAPNSCredentials()
    • IsMessageRateExceeded()
    • IsMismatchedCredential()
    • IsRegistrationTokenNotRegistered()
    • IsServerUnavailable()
    • IsTooManyTopics()
    • IsUnknown()

IID API 異動

iid 套件目前提供的所有錯誤處理函式都已淘汰。請改用 errorutils 套件中提供的相應錯誤處理函式。