[ERR_INVALID_ARG_TYPE]:第一个参数必须为字符串类型或Buffer的实例使用admin.authverifyIdToken

问题描述

我正在编写一个后端服务器(使用Node.js),以允许用户与Firebase进行交互。这是我到目前为止的内容

  • 我可以让用户通过管理员ADK创建他们的Firebase帐户(使用电子邮件和密码);
  • 我可以让用户通过REST API登录到他们的Firebase帐户(使用他们已经创建的电子邮件和密码);并返回其idToken和refreshToken。

但是,我真的很奇怪,每当用户登录时,他们将拥有一个全新的idToken(持续1小时);那么当新的idToken生成时,旧的会立即过期吗?因此,我认为我将使用firebase-admin SDK的verifyIdToken函数来执行我需要的操作。

问题是,即使当我输入新的idToken时,该函数也会失败。我真的不知道这是怎么回事。

如果我不执行捕获操作,则会出现以下错误

(node:9240) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received type number (1598554002)
at write_ (_http_outgoing.js:653:11)
at ServerResponse.write (_http_outgoing.js:621:15)
at D:\Viet Properties\Coding\ViMath\spreadsheet.js:315:19
at processticksAndRejections (internal/process/task_queues.js:97:5)
at async verifyid (D:\Viet Properties\Coding\ViMath\spreadsheet.js:314:5)
at async Server.<anonymous> (D:\Viet Properties\Coding\ViMath\spreadsheet.js:584:17)
(node:9240) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag`--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9240) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这是我调用函数

async function verifyid(data,serverres) {
   serverres.write(data.id);
   await admin.auth().verifyIdToken(data.id).then(function(token) {
       serverres.write(token.exp)
   }).catch(function(error) {
       serverres.write(`Error code: ${error.code} \r\nError message: ${error.message}`);
   });
};

主要代码在这里

var server = http.createServer(async function (req,res) {
    var urllink = url.parse(req.url,true);
    var urldata = urllink.query;

    if (Object.entries(urldata).length !== 0){
        switch(urldata.goal){
            //I dropped some cases here for clearance of the code
            case 'verifyid':
                await verifyid(urldata,res)
                res.end();
                break;
            default:
                res.write("Error: No action has been specified!")
                res.end();
        };
    };
  });


//initialize admin
var admin = require('firebase-admin');
var serviceAccount = require("./vimath_serviceAccountKey.json");
const { parse } = require('path');
const { userInfo } = require('os');

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),databaseURL: 'https://[my project].firebaseio.com'
});

server.listen(8080);

你们能看看吗?

非常感谢您

祝你有美好的一天,

enter image description here

enter image description here

解决方法

此行引发错误:

[Route("api/myRoute")]
[HttpPost]
    public async Task<IHttpActionResult> uploadExcelData
                (
                  [FromBody] Array excelRows
    )
            {
                 string[] aryExcelRowsObjects;

                 DataTable dataTable = new DataTable();
                 dataTable.Columns.Add(new DataColumn("name",typeof(string)));
                 dataTable.Columns.Add(new DataColumn("position",typeof(string)));
                 dataTable.Columns.Add(new DataColumn("sales",typeof(int)));

                 // this is the example I'm using and it no longer works as you can't use .Split() on an array. What should I change it to?
                 aryExcelRowsObjects = excelRows.Split(','); 

                // this is the example I'm using and I don't think it accounts for adding multiple columns into a row
                    foreach (string s in aryExcelRowsObjects)
                    {
                        dataTable.Rows.Add(s);
                    }

            }

该API需要一个字符串或一个Buffer,但是您要传递一个数字。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...