【问题标题】:Firebase Error:The photoURL field must be a valid URLFirebase 错误:photoURL 字段必须是有效的 URL
【发布时间】:2019-06-05 21:39:23
【问题描述】:

我正在将图片上传到 Firebase 存储,并希望将其图片保存为 Firebase 身份验证中的 photoURL。但是,当我在使用获取签名 URL 后去上传图像 URL 时,我收到错误

错误:photoURL 字段必须是有效的 URL。

我知道该 URL 是有效的,因为我已经从控制台输出了它。我曾尝试使用decodeURI,甚至查看firebase-admin-node 的源代码一直跟踪到一个名为auth-api-requests.ts 的文件,该文件在第252 行检查名为validator.isURL(request.photoUrl) 的函数中的URL这使我进入了定义函数validator.ts 的文件,该文件在此函数的第 152 行定义了.isURL(),检查是针对一串禁止字符执行的。我不想篡改 Firebase 源代码,但我找不到任何解决方案。应该有一个更简单的解决方案,用于从一个谷歌函数 .getSignedURL() 返回用作另一个 .updateUser({photoURL:} 中的参数),特别是考虑到不能再从谷歌云函数节点调用 firebase.getDownloadURL()。感谢您为解决此问题提供的任何帮助。

    var downloadURL = "";
   await admin.storage().bucket("gs://****firebase.appspot.com").file(storageRef).getSignedUrl({"action":"read","expires":Date.now() + 500*365*24*3600000}).then((value) => {
        console.log("value after requesting signed URL: " + JSON.stringify(value));
        downloadURL = value;
        return value;
    }).catch((error) => {
        console.log("error perfoming signed URL: " + error);
        return error;
    })


    const url = decodeURI(downloadURL)
    console.log("\nThe decodeURI url: " + url + "\n");

    await admin.auth().updateUser(userID,{photoURL:url}).then((user) => {
        console.log("User update ran a success: " + JSON.stringify(user));
        return true;
    }).catch((error) => {
        console.log("An error occured in getting the user: " + error);
        return error;
    });

【问题讨论】:

    标签: javascript firebase firebase-authentication google-cloud-storage google-cloud-functions


    【解决方案1】:

    硬编码用户 photoURL(仅为联合登录用户填充)不是一个好主意,因为它可能会发生变化。换句话说,Twitter 用户可能会更改他们的个人资料照片。 firebase.auth() 在用户登录时为您提供新的用户元数据。

    保存此类元数据只会增加维护开销 - 无需为此烦恼。

    【讨论】:

    • 感谢您的回复我从未真正想过它主要用于联合数据。您是对的,将 photoURL 附加到用户 auth() 配置文件并不能通过在 firebase 数据库中添加另一个节点来实现。
    猜你喜欢
    • 2016-09-19
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2018-04-06
    相关资源
    最近更新 更多