云功能。我们计算的请求签名与您提供的签名不匹配

时间:2018-03-04 11:59:16

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

我有一个任务,在应用程序中注册用户(通过Facebook注册)以将facebook头像保存在firebase存储中,因为facebook链接的工作时间有限。我实现了我在下面写的函数,但是我得到了以下错误

  

我们计算的请求签名与您提供的签名不符。检查您的Google密钥和签名方法

当我尝试使用指向图像的链接时。请告诉我它是如何修复的?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const gcs = require('@google-cloud/storage')({keyFilename: "service-account-credentials.json"});
const uuid = require('uuid');
const imageDownloader = require('../lib/Images/image-manager.js');
const path = require('path');
const timestamp = require('unix-timestamp');

module.exports = functions.https.onRequest((req, res) => {
    const token = req.header('X-Auth-MyApp-Token');
    const imageURL = req.body.imagePath;
    const bucketName = functions.config().googlecloud.defaultbacketname;
    const bucket = gcs.bucket(bucketName);

    var userID = '';
    const shortID = uuid.v1();
    const filename = shortID + '.jpg';
    var profileImagePath = '';

    return admin.auth().verifyIdToken(token).then(decodedToken => {
        userID = decodedToken.uid;

        return imageDownloader.downloadImageToLocalDirectory(imageURL, filename)
    }).then(localImagePath => {

        profileImagePath = path.normalize(path.join('userImages', userID, 'profileImages', filename));
        const uploadProm = bucket.upload(localImagePath, {
            destination: profileImagePath,
            uploadType: "media",
            metadata: {
                contentType: 'image/jpeg'
            }
        });

        return uploadProm;
        }).then(() => {
        console.log('success uploaded');
        const config = {
            action: 'read',
            expires: '03-01-2400',
            contentType: 'image/jpeg'
        };

        const userRefPromise = admin.database().ref()
            .child('users')
            .child(userID)
            .once('value');

        const profileImageFile = bucket.file(profileImagePath);

        return Promise.all([profileImageFile.getSignedUrl(config), userRefPromise])
        }).then(results => {
        const url = results[0][0];
        const userModel = results[1].val();
        const userCheckID = userModel['id'];

        console.log("get url", url);

        // save to database

        const userImagesRef = admin.database().ref().child('userImages')
            .child(userID)
            .child('userProfileImages')
            .push();

        const timeStamp = timestamp.now();
        console.log('timeStamp', timeStamp);
        const imageModelID = userImagesRef.key;
        const userImagesRefPromise = userImagesRef.update({
            'path': url,
            'id': imageModelID,
            'fileName': filename,
            'timeStamp': timeStamp
        });

        const userRef = admin.database().ref()
            .child('users')
            .child(userID)
            .child('currentProfileImage');
        const userRefPromise = userRef.update({
            'path': url,
            'id': imageModelID,
            'fileName': filename,
            'timeStamp': timeStamp
        });

        return Promise.all([userImagesRefPromise, userRefPromise]);
    }).then(() => {
        const successJSON = {};
        successJSON["message"] = "Success operation";
        return res.status(200).send(successJSON);
    }).catch(error => {
        console.log(error);
        const errorJSON = {};
        errorJSON["error"] = error;
        return res.status(error.code).send(errorJSON);
    });
});

0 个答案:

没有答案