【问题标题】:Firebase cloud messaging api v1 Expected OAuth 2 access tokenFirebase 云消息传递 API v1 预期的 OAuth 2 访问令牌
【发布时间】:2022-10-15 03:36:10
【问题描述】:

我猜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 后端和颤振。

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

Authorization = Bearer +AAAAA..token

这是我寄来的尸体

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

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

  @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


    【解决方案1】:

    我找到了解决我的问题的方法。我将把解决方案留给需要它的人。

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

    这是您可以测试的方法

    1 - 打开 OAuth 2.0 游乐场https://developers.google.com/oauthplayground/

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

    3-输入您的授权码(当您为您的用户界面进行配置时由firebase提供的设备令牌)并单击“交换令牌授权码”

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

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

    查看此 repo 以了解您的编程语言。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-14
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      • 2017-07-04
      相关资源
      最近更新 更多