我正在构建一个由Firebase应用支持的Android应用,我希望能够创建一个可以编辑或删除其他用户帐户的管理员帐户.如果我理解正确,Firebase Admin SDK应该允许我这样做.所以我按照这里的说明操作.
在我的应用中设置Admin SDK.我在build.app中添加了以下内容:
compile 'com.google.firebase:firebase-admin:4.1.1'
Run Code Online (Sandbox Code Playgroud)
在我的Application类中,我添加了这个:
FileInputStream serviceAccount = null;
try {
serviceAccount = new FileInputStream("app/<MY-DATABASE>.json");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (serviceAccount != null) {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://<MY-APP>.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
}
Run Code Online (Sandbox Code Playgroud)
但是,它告诉我:
根据文档,FirebaseOptions.Builder.setCredential()是一种新方法,它取代了已弃用的方法FirebaseOptions.Builder.setServiceAccount().但setServiceAccount()也不存在.
这里发生了什么?
我正在将firebase云功能与云功能上的javascript一起使用。而且,我想从javascript切换到打字稿。
但是,由于以下命令失败,因此无法在打字稿上使用firebase-admin。
command: npm install @types/firebase-admin --save-dev
error: '@types/firebase-admin' is not in the npm registry.
Run Code Online (Sandbox Code Playgroud)
根据此发行说明,firebase管理员支持打字稿。有人可以告诉我们如何在云功能上将typescript与firebase-admin一起使用吗?
https://firebase.google.com/support/release-notes/admin/node#4.1.3
我正在尝试通过Node.js后端将Instagram OAuth连接到Firebase.我已经成功地检索的Instagram帐户数据,包括access_token我想交换firebase-admin的createCustomToken在我的Node.js的后端.我的目标是生成自定义令牌,以便我的Angular应用可以signInWithCustomToken(token)在我的Firebase应用中执行.从Instagram检索数据没有问题,因为我可以在控制台上打印我的JSON对象.
当我想将我更换access_token为Firebase自定义令牌时,会出现此问题.
我从Firebase管理页面中查看了Node.js的本指南,我在下面面临一条错误消息
抛出新的错误_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_OPTIONS,"无效的Firebase应用程序选项作为第一个参数传递给initializeApp()为"+
错误:作为名为"[DEFAULT]"的应用的initializeApp()的第一个参数传递的Firebase应用选项无效."credential"属性必须是实现Credential接口的对象.
这是我在相关问题上的代码.
// authService.js
var fbAdmin = require('firebase-admin');
var serviceAccount = require('./key/key.json');
function createFirebaseToken(instagramID) {
// I copy & pasted this var from other class
var config = {
apiKey: "MY_FIREBASE_APIKEY",
authDomain: "MY_APP.firebaseapp.com",
databaseURL: "https://MY_APP.firebaseio.com",
storageBucket: "MY_APP.appspot.com",
};
console.log(fbAdmin.credential.cert(serviceAccount)); // serviceAccount successfully printed on console
// Error appears when executing this function
fbAdmin.initializeApp({
serviceAccount: fbAdmin.credential.cert(serviceAccount),
databaseURL: config.databaseURL
});
const uid = `instagram:${instagramID}`;
// …Run Code Online (Sandbox Code Playgroud)我尝试在云功能中使用Firestore,但我遇到了错误
db.collection(...).doc(...).collection(...).doc(...).add不是Promise的函数
我看了这些题目第一,第二等.但是id并没有帮助我.package.json看起来
"firebase": "^4.3.1",
"firebase-admin": "^5.5.1",
"firebase-functions": "^0.7.5",
Run Code Online (Sandbox Code Playgroud)
云功能之一
const admin = require('firebase-admin');
var db = admin.firestore();
Run Code Online (Sandbox Code Playgroud)
这个代码来自函数
const currentUserMatchProm = db.collection('userMatches').doc(currentUserID).collection('test').doc(matchID).add({
'matchID': matchID,
'matchedUserID': eventUserID,
'timestamp': timestamp,
'isActive': false
});
const eventUserMatchProm = db.collection('userMatches').doc(eventUserID).collection('test').doc(matchID).add({
'matchID': matchID,
'matchedUserID': currentUserID,
'timestamp': timestamp,
'isActive': false
});
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
当我尝试使用环境变量对Cloud Firestore进行身份验证时,我收到ENAMETOOLONG错误.我搜索了很多地方的文档.如果有人能指出我正确的方向,这将是有用的.
我已经创建了一个环境变量来加载我的服务帐户密钥
export MY_CREDENTIALS=$(cat myGoogleServiceAccountKey.json)
const admin = require('firebase-admin');
var serviceAccount = process.env.MY_CREDENTIALS;
// console.log(`Service account = ${serviceAccount}`);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const db = admin.firestore();
Run Code Online (Sandbox Code Playgroud)
当我运行从我的环境变量中获取serviceaccount键的节点脚本时,我收到以下错误:
/home/jason/Downloads/projects/myProject/functions/node_modules/firebase-admin/lib/auth/credential.js:142
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse certificate key file: ' + error);
^
Error: Failed to parse certificate key file: Error: ENAMETOOLONG: name too long, open '{
"type": "service_account",
"project_id": "myProject",
"private_key_id": "123456789012345678901234567890",
"private_key": "-----BEGIN PRIVATE KEY-----\nMy private key\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-6ju1c@myProject.iam.gserviceaccount.com",
"client_id": "12345678901234567890",
"auth_uri": "https://accounts.google.com/o/oauth2/auth", …Run Code Online (Sandbox Code Playgroud) google-authentication node.js firebase firebase-admin google-cloud-firestore
我在登录时在后端服务器上创建自定义令牌,下面是代码:
UserSchema.methods.generateAuthToken = function() {
var user = this;
var access = "auth";
return firebaseAdmin.default
.auth()
.createCustomToken(user._id.toHexString())
.then(function(token) {
user.tokens = user.tokens.concat([{ access, token }]);
return user.save().then(() => {
return token;
});
});
};
Run Code Online (Sandbox Code Playgroud)
并将此令牌存储在mongodb中,然后通过名为“ x-auth”的标头将其发送回客户端,然后将其存储在cookie中。
在客户端,使用此令牌登录如下所示:
axios({
url: "/api/users/login",
method: "POST",
data: {
email: data.email,
password: data.password
}
}).then(res => {
Cookies.set("x-auth", res.headers["x-auth"], { expires: 7 });
return firebase
.auth()
.signInWithCustomToken(res.headers["x-auth"])
.then(response => {
console.log("CA", response);
response
.getIdToken()
.then(idToken => {
Cookies.set("idToken", idToken, { expires: 7 });
})
.catch(e …Run Code Online (Sandbox Code Playgroud) javascript access-token firebase firebase-authentication firebase-admin
将图像保存到云存储后,我在firebase功能中获取下载URL时遇到问题.下面是我在firecript中的firebase http函数,用于将base64字符串保存到firebase云存储中的jpeg.当我记录结果时,它总是为空.我按照此链接获取下载URL" 从使用firebase-admin上传的文件中获取公共URL ".但它给了我以下错误:" SigningError:身份和访问管理(IAM)API尚未在项目81673989436中使用之前或它被禁用. "并且找不到简单的示例.我的计划是在上传到云存储后更新我的firestore表.
export const publishImage = functions.https.onRequest((req, res) => {
// Convert the base64 string back to an image to upload into the Google
// Cloud Storage bucket
const base64EncodedImageString=req.body.image.replace(/^data:image\/jpeg;base64,/, "");
const mimeType = 'image/jpeg';
const randomFileName = crypto.randomBytes(16).toString('hex') + '.jpeg';
const imageBuffer = new Buffer(base64EncodedImageString, 'base64');
const bucket = admin.storage().bucket();
// Upload the image to the bucket
const file = bucket.file('images/' + randomFileName);
file.save(imageBuffer, {
metadata: { contentType: mimeType },
}).then(result => {
console.log(result);
file.makePublic(); …Run Code Online (Sandbox Code Playgroud) 我正在使用google firebase admin sdk并按照说明进行设置.我通过FCM发送消息和通知,这没有问题,但最近我注意到我们的指标显示没有通知发送到客户端设备.
看看Node.js输出,我看到:
errorInfo:
{ code: 'messaging/invalid-apns-credentials',
message: 'Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.' },
codePrefix: 'messaging' }
Run Code Online (Sandbox Code Playgroud)
请记住我最初使用的凭证文件正在运行,并且环境中没有任何变化.
查看文档除了提供firebase管理员sdk要求开始的json文件之外,没有任何关于令牌过期或处理任何类型的身份验证:
var admin = require("firebase-admin");
var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://my-app-id.firebaseio.com"
});
Run Code Online (Sandbox Code Playgroud)
如果我登录firebase控制台并检查服务帐户,我可以看到它,我唯一的选择是生成一个新的私钥(大蓝色按钮).
我应该做些什么来让凭证保持有效?更新令牌或其他什么?在FCM设置文档中没有任何地方提到它,并且它似乎意味着如果你使用官方firebase管理员sdk则不需要任何这种性质.以下是我所遵循的指南:https://firebase.google.com/docs/admin/setup
任何人都可以权衡,如果我需要添加一些额外的代码来捕获证书到期和续订?
谢谢!
firebase身份验证和firebase管理员有什么区别?
据我所知,firebase管理员具有身份验证功能,并且可以绕过安全性,这与firebase身份验证不同。Firebase管理员是在服务器端实现的,而Firebase身份验证主要是在客户端。
我想知道为什么Firebase身份验证应该在客户端,为什么我们应该使用Firebase身份验证而不是Firebase admin进行身份验证功能。
我在终端中收到此错误:
@firebase/database: FIREBASE WARNING: {"code":"app/invalid-
credential","message":"Credential implementation provided to .
initializeApp() via the \"credential\" property failed to fetch a valid
Google OAuth2 access token with the following error: \"Failed to parse
access token response: Error: Error while making request: getaddrinfo
ENOTFOUND metadata.google.internal metadata.google.internal:80. Error
code: ENOTFOUND\"."}`
Run Code Online (Sandbox Code Playgroud)
使用时firebase-tools。这是我要运行的简单节点脚本。
const admin = require("firebase-admin");
const firebase = admin.initializeApp({
apiKey: "MY_API_KEY",
authDomain: "APP_ID.firebaseapp.com",
databaseURL: `https://DATABASE_URL.firebaseio.com`,
projectId: "APP_ID"
});
const snap = firebase
.database()
.ref("REF")
.child("ID")
.once("value");
console.log(snap);
Run Code Online (Sandbox Code Playgroud)
Firebase工具版本:5.0.1
我已经试过卸载并重新安装,进出的火力点,用工具登录firebase login/ …
firebase-admin ×10
firebase ×9
node.js ×4
javascript ×2
typescript ×2
access-token ×1
android ×1