如何解决具有自定义HTTP客户端的Firebase Go SDK
我一直在通过Go后端使用firebase云消息传递。 我工作场所的最新变化要求我为FCM使用自定义HTTP客户端。
如果我不使用自定义HTTP客户端,则可以向相关设备发送通知。
当我在NewApp
函数中使用WithHTTPClient选项时,出现如下错误
error sending notification Request had invalid authentication credentials. Expected OAuth 2 access token,login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
我附上了导致以下问题的代码。我为此使用Firebase 4.0。但是,如果有修复程序,我可以转到旧版本。 我正在使用我的服务帐户文件登录到客户端(使用GOOGLE_APPLICATION_CREDENTIALS)。
package main
import (
"context"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/messaging"
"fmt"
"google.golang.org/api/option"
"google.golang.org/api/transport/http"
"os"
"time"
)
func main() {
client,_,err := http.NewClient(context.Background())
app,err := firebase.NewApp(context.Background(),&firebase.Config{ProjectID:"Project ID",},option.WithHTTPClient(client),)
if err != nil {
fmt.Println("error creating app",err)
os.Exit(1)
}
messagingClient,err := app.Messaging(context.Background())
if err != nil {
fmt.Println("error initializing messagingClient",err)
os.Exit(2)
}
messageToSend := messaging.Message{
Data: nil,Notification: &messaging.Notification{
Title: "Hello",Body: "This is a test message",Token: "abcdefghijkl",}
reqId,sendErr := messagingClient.Send(context.Background(),&messageToSend)
if sendErr != nil {
fmt.Println("error sending notification",sendErr)
}
fmt.Println(reqId)
time.Sleep(5 * time.Second)
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com(将#修改为@)