Firebase 云消息传递 api v1 预期 OAuth 2 访问令牌

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

我猜 firebase 为新用户禁用了旧版 api。他们希望我们使用 v1 api。 当我尝试向邮递员发送通知时,我得到了这个。

 "error": {
        "code": 401,
        "message": "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.",
        "status": "UNAUTHENTICATED"
    }

我有服务帐户、服务器密钥等。但我现在不知道如何迁移。 我正在使用 dart-shelf 后端和 flutter。

对于邮递员,这是我的配置。

Authorization = Bearer +AAAAA..token

这是我发送的正文

{
  "message":{
     "token":"<generated token by flutter>",
     "notification":{
       "body":"This is an FCM notification message!",
       "title":"FCM Message"
     }
  }
}

这就是我在 flutter 上为特定设备生成令牌的方式。

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      _init();
    });
    var initializationSettingsAndroid =
        const AndroidInitializationSettings('ic_launcher');
    var initialzationSettingsAndroid =
        const AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettings =
        InitializationSettings(android: initialzationSettingsAndroid);
    flutterLocalNotificationsPlugin.initialize(initializationSettings);

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;
      if (notification != null && android != null) {
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                color: Colors.blue,
                // TODO add a proper drawable resource to android, for now using
                //      one that already exists in example app.
                icon: "@mipmap/ic_launcher",
              ),
            ));
      }
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;
      if (notification != null && android != null) {
        showDialog(
            context: context,
            builder: (_) {
              return AlertDialog(
                title: Text(notification.title!),
                content: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [Text(notification.body!)],
                  ),
                ),
              );
            });
      }
    });

    getToken();
  }

我的谷歌云计算后端。

firebase dart google-cloud-platform oauth-2.0 firebase-cloud-messaging
2个回答
2
投票

我找到了解决问题的方法。我将在这里留下解决方案供需要的人使用。

您的帖子标题和正文是正确的。您应该用您的授权码换取令牌。

这是您可以测试的方法

1 - 打开 OAuth 2.0 Playground https://developers.google.com/oauthplayground/

2- 输入您的范围,即 https://www.googleapis.com/auth/firebase.messaging (提交范围后,它将授权给您的 Google 帐户)

3- 输入您的授权代码(您为 UI 配置时由 firebase 提供的设备令牌),然后单击“将授权代码交换为令牌”

Playground 将为您提供访问令牌和刷新令牌。您应该将您的访问令牌放入 Bearer ya29....

当您能够通过邮递员发送通知后。但对于您的软件,您应该在代码中自动交换令牌。

查看此存储库以了解您的编程语言。

https://github.com/googleapis?q=auth


0
投票

我对不记名令牌感到疯狂:)

您发布的内容确实有效。但什么是? 它为我生成了一个有效但不与我的应用程序绑定的不记名令牌。您能向我解释一下正确的方法吗?谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.