Firebase密钥是在Firebase项目中使用身份验证和授权功能时使用的一组凭据
Firebase JSON文件的结构如下:
{
"type": "service_account",
"project_id": "your-project-id",
"private_key_id": "your-private-key-id",
"private_key": "-----BEGIN PRIVATE KEY-----\nYOUR_PRIVATE_KEY\n-----END PRIVATE KEY-----",
"client_email": "your-client-email@your-project-id.iam.gserviceaccount.com",
"client_id": "your-client-id",
"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/your-client-email%40your-project-id.iam.gserviceaccount.com"
}
请确保用您自己的Firebase项目值替换占位符。这些值可以在Firebase控制台中找到。
一旦您下载了Firebase JSON文件,请按照以下步骤使用它:
const admin = require('firebase-admin');
const serviceAccount = require('./path/to/firebase-key.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://your-database-url.firebaseio.com'
});
import firebase_admin
from firebase_admin import credentials
cred = credentials.Certificate('path/to/firebase-key.json')
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://your-database-url.firebaseio.com'
})
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
// ...
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("path/to/firebase-key.json"));
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(credentials)
.setDatabaseUrl("https://your-database-url.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
请确保将path/to/firebase-key.json
替换为您下载的JSON文件的本地路径,并将your-database-url
替换为您的实际数据库URL。
这样,您就可以使用Firebase密钥文件初始化Firebase Admin SDK,并访问Firebase数据库了。
领取专属 10元无门槛券
手把手带您无忧上云