400错误的请求错误订阅用户在云功能主题 [英] 400 Bad Request Error while subscribing users to topic in cloud function
本文介绍了400错误的请求错误订阅用户在云功能主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ p $
request({
method:'POST',
url:https://iid.googleapis.com/iid/v1:batchAdd,
json:true,
标题:{
'内容'type':'application / json',
'Authorization':'key = server key,
},
body:JSON.stringify({ b
$to:/ topics / name,
registration_tokens:[token value]
})
},function错误,响应,正文){
console.log('error:',error);
console.log('statusCode:',response&& response.statusCode);
if(response.statusCode> = 400){
console.error('HTTP Error:'+ response.statusCode +' - '+ response.statusMessage);
}
});
但是它提供了400个状态代码而不是200个。谢谢。
解决方案
我明显发现与订阅设备到主题不同的文档。 此文档表示使用如下格式的网址:
https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME
使用此方法的代码的变体如下所示。它适用于我。但是,使用管理员并不是那么简单。 messaging()。subscribeToTopic()?
const token ='f_PWaiHMGvQ:APA91bGN ... 9F5D5avIpjY57Y098OFsxZLHUZubx0P_';
const serverKey ='AAAAyq9marw:APA91b ..._ Xf-jV472nfn-sb';
request({
method:'POST',
url:`https://iid.googleapis.com/iid/v1/$ {token} / rel / topics / myTopic`,
头文件:{
'Content-Type':'application / json',
'Content-Length':0,
'Authorization':`key = $ {serverKey} `,
}
},函数(error,response,body){
console.log('error:',error);
console.log('statusCode:' ,response&& response.statusCode);
if(response.statusCode> = 400){
console.error('HTTP Error:'+ response.statusCode +' - '+ response.statusMessage );
}
});
I'm trying to subscribe users to firebase topic on the cloud function. So according to official docs I tried the following code
request({
method: 'POST',
url: "https://iid.googleapis.com/iid/v1:batchAdd",
json: true,
headers: {
'Content-Type':'application/json',
'Authorization':'key=server key,
},
body: JSON.stringify({
"to": "/topics/name",
"registration_tokens": ["token value"]
})
}, function (error, response, body){
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
if (response.statusCode >= 400) {
console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage);
}
});
But it's giving 400 status code instead of 200. Thank you.
解决方案
I apparently found different documentation than you did for subscribing a device to a topic. This documentation says to use a URL formatted like this:
https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME
A variation on your code that uses this method is below. It works for me. But wouldn't it be much simpler to use admin.messaging().subscribeToTopic()?
const token = 'f_PWaiHMGvQ:APA91bGN...9F5D5avIpjY57Y098OFsxZLHUZubx0P_';
const serverKey = 'AAAAyq9marw:APA91b..._Xf-jV472nfn-sb';
request({
method: 'POST',
url: `https://iid.googleapis.com/iid/v1/${token}/rel/topics/myTopic`,
headers: {
'Content-Type':'application/json',
'Content-Length': 0,
'Authorization':`key=${serverKey}`,
}
}, function (error, response, body){
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
if (response.statusCode >= 400) {
console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage);
}
});
这篇关于400错误的请求错误订阅用户在云功能主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文