本文介绍了googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken返回CREDENTIAL_MISMATCH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终目标:使用本地NodeJ创建一个Firebase自定义令牌,将该自定义令牌发布到googleapis/.../verifyCustomToken并返回一个idToken,该IDToken允许我按照

Final Goal: create a Firebase Custom Token with local NodeJs, post such Custom Token to googleapis/.../verifyCustomToken and get back an idToken that allow me to post a new document to Firestore as explained in other question

当前成果:我可以使用此Cloud Function成功创建Custon令牌,并成功地进行后续步骤(从googleapis/.../verifyCustomToken中获取idToken并将文档成功发布到Firestore).但是我需要从本地服务器获取相同的信息(这次我们将不再使用Cloud Function).

Current achivement: I can successfuly create a Custon Token with this Cloud Function and follow successfully next steps (get an idToken from googleapis/.../verifyCustomToken and post successfully a docuemtn to Firestore). But I need the same from a local server (we are not going to use Cloud Function this time).

当前问题:当我尝试将本地NodeJ生成的自定义令牌发布到googleapis/.../verifyCustomToken时,会遇到此问题:

Current issue: I get this issue when I try to post the Custom Token generated by local NodeJs to googleapis/.../verifyCustomToken:

{
  "error": {
    "code": 400,
    "message": "CREDENTIAL_MISMATCH",
    "errors": [
      {
        "message": "CREDENTIAL_MISMATCH",
        "domain": "global",
        "reason": "invalid"
      }
    ]
  }
}

这是整个NodeJs服务器:

Here is the whole NodeJs server:

const admin = require('firebase-admin');

exports.serviceAccount = {
  "type": "service_account",
  "project_id": "angular-firebase-auth0-3c084",
  "private_key_id": "6ba2ba41e0bf3837841aa9772c7d880b7ce3be81",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMI... 9fYKA=\n-----END PRIVATE KEY-----\n".replace(/\\n/g, '\n'),
  "client_email": "firebase-adminsdk-lu97a@angular-firebase-auth0-3c084.iam.gserviceaccount.com",
  "client_id": "114324662014690107039",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-lu97a%40angular-firebase-auth0-3c084.iam.gserviceaccount.com"
}

 admin.initializeApp({
   credential: admin.credential.cert(exports.serviceAccount)
});

var uid = "NSBFu2YJNDgLQJCZ99dRJlP4DRo2"; //copied from https://console.firebase.google.com/project/firetestjimis/authentication/users
var claim = {
  control: true
};
admin.auth().createCustomToken(uid)
  .then(function (customToken) {
    console.log(customToken)
  })
  .catch(function (error) {
    console.log("Error creating custom token:", error);
  });

我复制打印的令牌并尝试:

I copy the printed token and I try:

curl --location --request POST 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=AIzaSyDAd03oo5fPgV2l--oMWZ2Y23DCGihK3xs' \
--header 'Content-Type: application/json' \
--data-raw '{"token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJodHRwczovL2lkZW50aXR5dG9vbGtpdC5nb29nbGVhcGlzLmNvbS9nb29nbGUuaWRlbnRpdHkuaWRlbnRpdHl0b29sa2l0LnYxLklkZW50aXR5VG9vbGtpdCIsImlhdCI6MTU4NzQ4NjI0NiwiZXhwIjoxNTg3NDg5ODQ2LCJpc3MiOiJmaXJlYmFzZS1hZG1pbnNkay1sdTk3YUBhbmd1bGFyLWZpcmViYXNlLWF1dGgwLTNjMDg0LmlhbS5nc2VydmljZWFjY291bnQuY29tIiwic3ViIjoiZmlyZWJhc2UtYWRtaW5zZGstbHU5N2FAYW5ndWxhci1maXJlYmFzZS1hdXRoMC0zYzA4NC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsInVpZCI6Ik5TQkZ1MllKTkRnTFFKQ1o5OWRSSmxQNERSbzIifQ.fSe8ONIHBgoeVg4OkTSemykq4RoFb5TOiHOq52zKiiXCfywmdGAtqZyAWfM_dz-knP4mbSyJM9N3T2A_GQ0fV6AGTlq3lalDaptQPfYX4B7MOiA6YODJSDXGyGVHbdF88MmtNzESszbivF7RoFTBanyawVa9dwy83-84_2nJHylqmq055oFurd-WkM-gnfjyRBvGzQmZp7l76dV1rzRiKg8_ctiO8SOwD84KriXQj6DL-LFze7wb6XJSCJ52epXH0FvjALsB4R1eqCDHAJ3COfEYWiE0Vn5LWhj6yFvtSG3vqLqXy79EDkoXVPw0IJNiBSE4e3gfmat12M9peJEoTw","returnSecureToken":true}'

然后返回到CREDENTIAL_MISMATCH.

and then result to CREDENTIAL_MISMATCH.

在相关的情况下,如果我从此Cloud Funtion中生成了自定义令牌,则可以成功发布以验证verifyCustomToken

In case it is relevant, if I generated the Custom Token from this Cloud Funtion I can post successfully to verifyCustomToken

import * as functions from 'firebase-functions';
import * as admin from "firebase-admin";

export const getCustomToken = functions.https.onRequest((request, response) => {
    if (admin.apps.length < 1) {   //Checks if app already initialized
        admin.initializeApp();
    }
    const uid = "NSBFu2YJNDgLQJCZ99dRJlP4DRo2"; 

    admin.auth().createCustomToken(uid)
        .then(function (customToken) {
            console.log(customToken.toString);
            response.send(customToken);
        })
        .catch(function (error) {
            console.log("Error creating custom token:", error);
        });
});

推荐答案

确保URL中发送的API密钥与用于铸造自定义令牌的服务帐户来自同一项目.

Make sure the API key sent in the URL came from the same project as the service account used to mint the custom token.

这篇关于googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken返回CREDENTIAL_MISMATCH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:56