FCM从Legacy迁移至V1 API时遇401认证缺失错误求助
FCM V1 API调用401认证错误排查
我们后端通过FCM向移动应用发送支付通知,因2024年7月Legacy API停用,按Google官方迁移指南升级至V1版本。编写了新的sendFCMInvoicePaidNotificationMessagev2函数调用FCM V1 API,但运行时返回401错误,提示「请求缺失必要认证凭证,需OAuth 2访问令牌」。已按步骤生成service_account.json服务账号密钥,仍无法解决问题,请求排查原因。
代码实现
async function sendFCMInvoicePaidNotificationMessagev2(to, transactionID, mobileNumber, callback) { var myAccessToken = await getAccessToken() unirest.post('https://fcm.googleapis.com/v1/projects/xxxx-xxxx-xxxx/messages:send').headers({ 'Authorization': myAccessToken, 'Accept': 'application/json', 'Content-Type': 'application/json', 'cache-control': 'no-cache' }).proxy('http://xxxx:1234').send({ 'to': to, 'priority': 'high', 'restricted_package_name': config.get('googlePkgName'), 'notification': { 'title': 'Invoice # ' + transactionID + ' has been paid. Thank you.' }, 'data': { 'transactionID': transactionID, 'mobileNumber': mobileNumber } }).end(function(responseFCM) { callback(responseFCM); }); } function getAccessToken() { var SCOPES = ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/firebase.messaging']; return new Promise(function(resolve, reject) { const key = require('./service_account.json'); const jwtClient = new google.auth.JWT( key.client_email, null, key.private_key, SCOPES, null ); jwtClient.authorize(function(err, tokens) { if (err) { reject(err); return; } resolve(tokens.access_token); }); }); }
错误返回信息
{"statusCode":401,"body":{"error":{"code":401,"message":"Request is missing required authentication credential. 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","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"CREDENTIALS_MISSING","domain":"googleapis.com","metadata":{"method":"google.firebase.fcm.v1.FcmService.SendMessage","service":"fcm.googleapis.com"}}]}},"headers":{"www-authenticate":"Bearer realm=\"https://accounts.google.com/\"","vary":"X-Origin, Referer, Origin,Accept-Encoding","content-type":"application/json; charset=UTF-8","date":"Tue, 17 Sep 2024 07:14:13 GMT","server":"scaffolding on HTTPServer2","cache-control":"private","x-xss-protection":"0","x-frame-options":"SAMEORIGIN","x-content-type-options":"nosniff","alt-svc":"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000","accept-ranges":"none","connection":"close","transfer-encoding":"chunked"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"host":"fcm.googleapis.com","port":443,"hostname":"fcm.googleapis.com","hash":null,"search":null,"query":null,"pathname":"/v1/projects/xxxxxxxx/messages:send","path":"/v1/projects/xxxxxxxx/messages:send","href":"https://fcm.googleapis.com/v1/projects/xxxxxxxx/messages:send"},"method":"post","headers":{"Authorization":{},"Accept":"application/json","Content-Type":"application/json","cache-control":"no-cache","content-length":386}}}
已执行的操作步骤
- 在Firebase控制台进入设置>服务账号
- 点击「生成新私钥」并确认
- 安全存储生成的JSON密钥文件
内容的提问来源于stack exchange,提问作者anta40
相关产品推荐
相关产品推荐

