GoogleAuth的不正确的Metadata-Flavor标头是什么意思?

时间:2019-05-31 22:15:08

标签: node.js firebase google-cloud-firestore google-cloud-functions

我正在尝试通过onCall Firebase函数写入Firestore

functions.js

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


admin.initializeApp();



console.log('initialing functions at ' , new Date().toString())


exports.getLinks = functions.runWith({ timeoutSeconds: 540 }).https.onCall( (data,context) => {

    console.log('starting to get links ' , new Date().toString())
    console.log('data' , data.query, data.limit, data.country, data.uid)
    console.log('context auth', context.auth, 'context.auth.uid', context.auth.uid)
    // is there anything like admin.setCredentials(context.auth) necessary here?
    const queries = admin.firestore().collection('queries');
    let uid = data.uid
    console.log('uid', uid);
    console.log('queries ref', queries)
    //probably when trying to write here is not being allowed
    queries.doc(uid).set({LinksArrayLength: 'starting'})
        .then( r => console.log('writing to firestore 1 result', r))
        .catch( err => console.error('writing to firestore 2 error', err))

控制台输出是这样的

  

开始获取链接2019年5月31日星期五19:01:10 GMT-0300(GMT-03:00)

     
    

数据随处可见2 com fwfwqe      上下文验证{        uid:“ f23oij2ioafOIJOeofiwjOIJ”,        令牌:{          iss:“ https://securetoken.google.com/was98oinr-fa4c9”,          aud:“ was234r-f32c9”,          auth_time:1559327744,          user_id:“ f23oij2ioafOIJOeofiwjOIJ”,          子:“ f23oij2ioafOIJOeofiwjOIJ”,          iat:1559338208,          exp:1559341808,          电子邮件:“ awef3h@gmail.com”,          email_verified:否,          firebase:{身份:[Object],sign_in_provider:'password'},          uid:“ f23oij2ioafOIJOeofiwjOIJ”        }      } context.auth.uid f23oij2ioafOIJOeofiwjOIJ      uid f1EMxzwjJlTaH3u7RAYsySx0MZV2      查询ref CollectionReference {        _firestore:Firestore {          _设置:{            projectId:“ xxx”,            firebaseVersion:'7.0.0',            libName:'gccl',            libVersion:'1.3.0火/7.0.0'          }

  

,然后是不允许向Firestore的写入请求?

writing to firestore 2 error Error: Unexpected error determining execution environment: Invalid response from metadata service: incorrect Metadata-Flavor header.
>      at GoogleAuth.<anonymous> (H:\nprojetos\whats_app_sender\firebase_sender\vue_sender\wa_sender\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:164:23)
>      at Generator.throw (<anonymous>)
>      at rejected (H:\nprojetos\whats_app_sender\firebase_sender\vue_sender\wa_sender\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:20:65)
>      at processTicksAndRejections (internal/process/task_queues.js:89:5)

如何确保将request.auth.uid发送到Firestore写入请求?

Firestore规则

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;//: if request.auth.uid != null;
// even when commented and allowing all requests still giving the error //message
    }
  }
}

即使在完全允许的情况下,仍然会出现错误。

我正在尝试写入firestore以在向collections('queries')中写入某些内容时更新客户端...以便使客户端收到有关函数进度的通知... 还有更好的方法吗?

在客户端,代码像这样

客户端

fireApp.firestore().collection('queries').doc(this.getUser.uid).onSnapshot(snap => {
                    debugger
                    console.log('snap', snap)

                    snap.exists ?
                    snap.docChanges().forEach(async change => {
                        if (change.type === "modified") {
                         _vue.updating = true // the function is in progress

1 个答案:

答案 0 :(得分:0)

已解决 它只需要适当的初始化

const credential = require('./xxxxx.json')

admin.initializeApp({credential: admin.credential.cert(credential),
databaseURL: "https://xxx.xxx.firebaseio.com"
});

此链接(https://www.youtube.com/watch?v=Z87OZtIYC_0) 解释了如何正确初始化