從版本 4.0.0 開始,Firebase Admin SDK for Go 已選擇加入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.13.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(v4 或更高版本),其中包含其他重大 API 變更。
一般錯誤處理更改
v4 SDK 引入了一個新的errorutils
包,該包提供了處理平台級錯誤情況的函數。如果錯誤是由後端服務錯誤引起的,您可以透過呼叫新函數errorutils.HTTPResponse()
來存取原始錯誤回應。當 SDK 中的任何 API 傳回錯誤時,您可以使用此套件中的函數。
身份驗證 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
包中提供的對應錯誤處理函數。