व्यवस्थापक SDK के साथ, आप पूर्ण व्यवस्थापकीय विशेषाधिकारों के साथ, या सीमित सीमित विशेषाधिकारों के साथ रीयलटाइम डेटाबेस डेटा पढ़ और लिख सकते हैं। इस दस्तावेज़ में, हम Firebase रीयलटाइम डेटाबेस तक पहुँचने के लिए आपके प्रोजेक्ट में Firebase Admin SDK जोड़कर आपका मार्गदर्शन करेंगे।
व्यवस्थापक एसडीके सेटअप
अपने सर्वर पर फायरबेस रीयलटाइम डेटाबेस के साथ आरंभ करने के लिए, आपको सबसे पहले अपनी पसंद की भाषा में फायरबेस एडमिन एसडीके सेट करना होगा।
व्यवस्थापक एसडीके प्रमाणीकरण
इससे पहले कि आप Firebase Admin SDK का उपयोग करके किसी सर्वर से Firebase रीयलटाइम डेटाबेस तक पहुँच सकें, आपको अपने सर्वर को Firebase से प्रमाणित करना होगा। जब आप एक सर्वर को प्रमाणित करते हैं, बजाय एक उपयोगकर्ता खाते के क्रेडेंशियल्स के साथ साइन इन करने के बजाय जैसा कि आप क्लाइंट ऐप में करते हैं, तो आप एक सेवा खाते से प्रमाणित करते हैं जो आपके सर्वर को फायरबेस से पहचानता है।
जब आप Firebase Admin SDK का उपयोग करके प्रमाणित करते हैं, तो आप एक्सेस के दो अलग-अलग स्तर प्राप्त कर सकते हैं:
फायरबेस एडमिन एसडीके ऑथेंटिक एक्सेस लेवल | |
---|---|
प्रशासनिक विशेषाधिकार | किसी प्रोजेक्ट के रीयलटाइम डेटाबेस तक पूर्ण पढ़ने और लिखने की पहुंच। डेटा माइग्रेशन या पुनर्गठन जैसे व्यवस्थापकीय कार्यों को पूरा करने के लिए सावधानी से उपयोग करें, जिनके लिए आपके प्रोजेक्ट के संसाधनों तक अप्रतिबंधित पहुंच की आवश्यकता होती है। |
सीमित विशेषाधिकार | किसी प्रोजेक्ट के रीयलटाइम डेटाबेस तक पहुंच, केवल आपके सर्वर के लिए आवश्यक संसाधनों तक सीमित। इस स्तर का उपयोग उन प्रशासनिक कार्यों को पूरा करने के लिए करें जिनकी पहुँच आवश्यकताओं को अच्छी तरह से परिभाषित किया गया है। उदाहरण के लिए, एक संक्षिप्तीकरण कार्य चलाते समय जो पूरे डेटाबेस में डेटा पढ़ता है, आप केवल-पढ़ने के लिए सुरक्षा नियम सेट करके और फिर उस नियम द्वारा सीमित विशेषाधिकारों के साथ व्यवस्थापक SDK को आरंभ करके आकस्मिक लेखन से बचा सकते हैं। |
व्यवस्थापक विशेषाधिकारों के साथ प्रमाणित करें
जब आप अपने Firebase प्रोजेक्ट पर संपादक की भूमिका वाले सेवा खाते के लिए क्रेडेंशियल के साथ Firebase Admin SDK को इनिशियलाइज़ करते हैं, तो उस इंस्टेंस के पास आपके प्रोजेक्ट के रीयलटाइम डेटाबेस तक पढ़ने और लिखने की पूर्ण पहुँच होती है।
जावा
// Fetch the service account key JSON file contents FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccount.json"); // Initialize the app with a service account, granting admin privileges FirebaseOptions options = FirebaseOptions.builder() .setCredentials(GoogleCredentials.fromStream(serviceAccount)) // The database URL depends on the location of the database .setDatabaseUrl("https://DATABASE_NAME.firebaseio.com") .build(); FirebaseApp.initializeApp(options); // As an admin, the app has access to read and write all data, regardless of Security Rules DatabaseReference ref = FirebaseDatabase.getInstance() .getReference("restricted_access/secret_document"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Object document = dataSnapshot.getValue(); System.out.println(document); } @Override public void onCancelled(DatabaseError error) { } });
नोड.जेएस
var admin = require("firebase-admin"); // Fetch the service account key JSON file contents var serviceAccount = require("path/to/serviceAccountKey.json"); // Initialize the app with a service account, granting admin privileges admin.initializeApp({ credential: admin.credential.cert(serviceAccount), // The database URL depends on the location of the database databaseURL: "https://DATABASE_NAME.firebaseio.com" }); // As an admin, the app has access to read and write all data, regardless of Security Rules var db = admin.database(); var ref = db.ref("restricted_access/secret_document"); ref.once("value", function(snapshot) { console.log(snapshot.val()); });
अजगर
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents cred = credentials.Certificate('path/to/serviceAccountKey.json') # Initialize the app with a service account, granting admin privileges firebase_admin.initialize_app(cred, { 'databaseURL': 'https://databaseName.firebaseio.com' }) # As an admin, the app has access to read and write all data, regradless of Security Rules ref = db.reference('restricted_access/secret_document') print(ref.get())
जाना
ctx := context.Background() conf := &firebase.Config{ DatabaseURL: "https://databaseName.firebaseio.com", } // Fetch the service account key JSON file contents opt := option.WithCredentialsFile("path/to/serviceAccountKey.json") // Initialize the app with a service account, granting admin privileges app, err := firebase.NewApp(ctx, conf, opt) if err != nil { log.Fatalln("Error initializing app:", err) } client, err := app.Database(ctx) if err != nil { log.Fatalln("Error initializing database client:", err) } // As an admin, the app has access to read and write all data, regradless of Security Rules ref := client.NewRef("restricted_access/secret_document") var data map[string]interface{} if err := ref.Get(ctx, &data); err != nil { log.Fatalln("Error reading from database:", err) } fmt.Println(data)
सीमित विशेषाधिकारों के साथ प्रमाणित करें
एक सर्वोत्तम प्रथा के रूप में, एक सेवा की पहुँच केवल उन्हीं संसाधनों तक होनी चाहिए जिनकी उसे आवश्यकता है। संसाधनों पर अधिक सूक्ष्म नियंत्रण प्राप्त करने के लिए एक फायरबेस ऐप इंस्टेंस एक्सेस कर सकता है, अपनी सेवा का प्रतिनिधित्व करने के लिए अपने सुरक्षा नियमों में एक अद्वितीय पहचानकर्ता का उपयोग करें। फिर उपयुक्त नियम सेट करें जो आपकी सेवा को आवश्यक संसाधनों तक पहुँच प्रदान करते हैं। उदाहरण के लिए:
{ "rules": { "public_resource": { ".read": true, ".write": true }, "some_resource": { ".read": "auth.uid === 'my-service-worker'", ".write": false }, "another_resource": { ".read": "auth.uid === 'my-service-worker'", ".write": "auth.uid === 'my-service-worker'" } } }
फिर, अपने सर्वर पर, जब आप फायरबेस ऐप को इनिशियलाइज़ करते हैं, तो अपने डेटाबेस नियमों द्वारा उपयोग की जाने वाली auth
ऑब्जेक्ट को ओवरराइड करने के लिए databaseAuthVariableOverride
विकल्प का उपयोग करें। इस कस्टम auth
ऑब्जेक्ट में, uid
फ़ील्ड को उस पहचानकर्ता पर सेट करें जिसका उपयोग आपने अपने सुरक्षा नियमों में अपनी सेवा का प्रतिनिधित्व करने के लिए किया था।
जावा
// Fetch the service account key JSON file contents FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountCredentials.json"); // Initialize the app with a custom auth variable, limiting the server's access Map<String, Object> auth = new HashMap<String, Object>(); auth.put("uid", "my-service-worker"); FirebaseOptions options = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) // The database URL depends on the location of the database .setDatabaseUrl("https://DATABASE_NAME.firebaseio.com") .setDatabaseAuthVariableOverride(auth) .build(); FirebaseApp.initializeApp(options); // The app only has access as defined in the Security Rules DatabaseReference ref = FirebaseDatabase .getInstance() .getReference("/some_resource"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String res = dataSnapshot.getValue(); System.out.println(res); } });
नोड.जेएस
var admin = require("firebase-admin"); // Fetch the service account key JSON file contents var serviceAccount = require("path/to/serviceAccountKey.json"); // Initialize the app with a custom auth variable, limiting the server's access admin.initializeApp({ credential: admin.credential.cert(serviceAccount), // The database URL depends on the location of the database databaseURL: "https://DATABASE_NAME.firebaseio.com", databaseAuthVariableOverride: { uid: "my-service-worker" } }); // The app only has access as defined in the Security Rules var db = admin.database(); var ref = db.ref("/some_resource"); ref.once("value", function(snapshot) { console.log(snapshot.val()); });
अजगर
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents cred = credentials.Certificate('path/to/serviceAccountKey.json') # Initialize the app with a custom auth variable, limiting the server's access firebase_admin.initialize_app(cred, { 'databaseURL': 'https://databaseName.firebaseio.com', 'databaseAuthVariableOverride': { 'uid': 'my-service-worker' } }) # The app only has access as defined in the Security Rules ref = db.reference('/some_resource') print(ref.get())
जाना
ctx := context.Background() // Initialize the app with a custom auth variable, limiting the server's access ao := map[string]interface{}{"uid": "my-service-worker"} conf := &firebase.Config{ DatabaseURL: "https://databaseName.firebaseio.com", AuthOverride: &ao, } // Fetch the service account key JSON file contents opt := option.WithCredentialsFile("path/to/serviceAccountKey.json") app, err := firebase.NewApp(ctx, conf, opt) if err != nil { log.Fatalln("Error initializing app:", err) } client, err := app.Database(ctx) if err != nil { log.Fatalln("Error initializing database client:", err) } // The app only has access as defined in the Security Rules ref := client.NewRef("/some_resource") var data map[string]interface{} if err := ref.Get(ctx, &data); err != nil { log.Fatalln("Error reading from database:", err) } fmt.Println(data)
कुछ मामलों में, हो सकता है कि आप एक अप्रमाणीकृत क्लाइंट के रूप में कार्य करने के लिए व्यवस्थापक SDKs को कम करना चाहें। आप डेटाबेस ऑथ वैरिएबल ओवरराइड के लिए null
का मान प्रदान करके ऐसा कर सकते हैं।
जावा
// Fetch the service account key JSON file contents FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountCredentials.json"); FirebaseOptions options = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) // The database URL depends on the location of the database .setDatabaseUrl("https://DATABASE_NAME.firebaseio.com") .setDatabaseAuthVariableOverride(null) .build(); FirebaseApp.initializeApp(options); // The app only has access to public data as defined in the Security Rules DatabaseReference ref = FirebaseDatabase .getInstance() .getReference("/public_resource"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String res = dataSnapshot.getValue(); System.out.println(res); } });
नोड.जेएस
var admin = require("firebase-admin"); // Fetch the service account key JSON file contents var serviceAccount = require("path/to/serviceAccountKey.json"); // Initialize the app with a null auth variable, limiting the server's access admin.initializeApp({ credential: admin.credential.cert(serviceAccount), // The database URL depends on the location of the database databaseURL: "https://DATABASE_NAME.firebaseio.com", databaseAuthVariableOverride: null }); // The app only has access to public data as defined in the Security Rules var db = admin.database(); var ref = db.ref("/public_resource"); ref.once("value", function(snapshot) { console.log(snapshot.val()); });
अजगर
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents cred = credentials.Certificate('path/to/serviceAccountKey.json') # Initialize the app with a None auth variable, limiting the server's access firebase_admin.initialize_app(cred, { 'databaseURL': 'https://databaseName.firebaseio.com', 'databaseAuthVariableOverride': None }) # The app only has access to public data as defined in the Security Rules ref = db.reference('/public_resource') print(ref.get())
जाना
ctx := context.Background() // Initialize the app with a nil auth variable, limiting the server's access var nilMap map[string]interface{} conf := &firebase.Config{ DatabaseURL: "https://databaseName.firebaseio.com", AuthOverride: &nilMap, } // Fetch the service account key JSON file contents opt := option.WithCredentialsFile("path/to/serviceAccountKey.json") app, err := firebase.NewApp(ctx, conf, opt) if err != nil { log.Fatalln("Error initializing app:", err) } client, err := app.Database(ctx) if err != nil { log.Fatalln("Error initializing database client:", err) } // The app only has access to public data as defined in the Security Rules ref := client.NewRef("/some_resource") var data map[string]interface{} if err := ref.Get(ctx, &data); err != nil { log.Fatalln("Error reading from database:", err) } fmt.Println(data)
अगले कदम
- रीयलटाइम डेटाबेस के लिए डेटा की संरचना करना सीखें।
- एकाधिक डेटाबेस उदाहरणों में डेटा स्केल करें ।
- डेटा सहेजें।
- डेटा पुनः प्राप्त करो।
- फायरबेस कंसोल में अपना डेटाबेस देखें।