Firebase云消息传递API v1 期望OAuth 2访问令牌。

3

我猜 Firebase 禁用了遗留 API,对于新用户来说。他们希望我们使用 v1 API。当我尝试使用 Postman 发送通知时,我得到了这个错误信息。

 "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。

对于Postman,这是我的配置。

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();
  }

我的后端托管在谷歌云计算上。

1个回答

2
我找到了解决问题的方法,现在我将把它留在这里,供需要的人使用。
您的帖子的标题和正文都是正确的。您应该用授权码换取令牌。
以下是测试的方法:
1- 打开 OAuth 2.0 Playground(https://developers.google.com/oauthplayground/
2- 输入您的范围,即 https://www.googleapis.com/auth/firebase.messaging (提交范围后,它会授权到您的 Google 帐户)
3- 输入您的授权码(由 Firebase 在为您的 UI 进行配置时提供的设备令牌),然后点击“将授权码交换为令牌”
Playground 将给您访问令牌和刷新令牌。您应该像 Bearer ya29... 这样放置您的访问令牌。
在您能够通过 Postman 发送通知之后。但对于您的软件,您应该自动化代码中的令牌交换。
检查此存储库是否适用于您的编程语言。 https://github.com/googleapis?q=auth

游乐场不适用于生产。 - Şahmirzəli Hüseynov
是的,这就是为什么我声明了 GitHub 存储库用于生产的原因。 - demonjoseph

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接