我猜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"
}
我有服务帐户、服务器密钥等。但我现在不知道如何迁移。我正在使用镖架后端和颤振。
对于邮递员来说,这是我的配置。
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-Open OAuth 2.0游乐场https://developers.google.com/oauthplayground/
2-输入您的范围https://www.googleapis.com/auth/firebase.messaging(在你提交你的范围后,它将授权给你的谷歌帐户(
3-输入您的授权码(当您为UI进行配置时,firebase提供的设备令牌(,然后单击";代币的交换授权码";
游乐场将为您提供访问令牌和刷新令牌。你应该把你的访问令牌像Bearer ya29…
之后你可以用邮递员发送通知。但对于您的软件,您应该在代码中自动交换令牌。
查看此repo了解您的编程语言。
https://github.com/googleapis?q=auth