एडमिन डेटाबेस एपीआई के बारे में जानकारी

एडमिन SDK से, रीयल टाइम डेटाबेस के डेटा को एडमिन के सभी खास अधिकारों के साथ पढ़ा और लिखा जा सकता है, सीमित सुविधाओं का ऐक्सेस मिलता है. इस दस्तावेज़ में, Firebase एडमिन SDK टूल जोड़ने का तरीका बताया गया है को ऐक्सेस करने के लिए, Firebase रीयल टाइम डेटाबेस को अपने प्रोजेक्ट में जोड़ें.

एडमिन SDK टूल का सेटअप

अपने सर्वर पर Firebase रीयल टाइम डेटाबेस के साथ शुरुआत करने के लिए, आपको पहले Firebase सेट अप) एडमिन SDK का इस्तेमाल करें.

एडमिन SDK टूल की पुष्टि करना

Firebase एडमिन SDK टूल का इस्तेमाल करके, किसी सर्वर से Firebase रीयल टाइम डेटाबेस को ऐक्सेस करने से पहले, आपको ये काम करने होंगे Firebase की मदद से अपने सर्वर की पुष्टि करें. जब आप साइन इन करने के बजाय किसी सर्वर को के क्रेडेंशियल, जैसा कि आप किसी क्लाइंट ऐप्लिकेशन में करेंगे, आपको सेवा खाता जो Firebase के लिए आपके सर्वर की पहचान करती है.

Firebase एडमिन SDK टूल:

Firebase एडमिन SDK टूल के लिए ऑथराइज़ेशन ऐक्सेस के लेवल
एडमिन के अधिकार प्रोजेक्ट के रीयलटाइम डेटाबेस के लिए, पढ़ने और लिखने का ऐक्सेस पूरा करें. इसके साथ इस्तेमाल करें एडमिन से जुड़े कामों को पूरा करने में सावधानी बरतनी चाहिए, जैसे कि डेटा दूसरी जगह भेजना या उसको दोबारा बनाना आपके प्रोजेक्ट के संसाधनों का बिना पाबंदी वाला ऐक्सेस होना ज़रूरी है.
सीमित अधिकार किसी प्रोजेक्ट के रीयलटाइम डेटाबेस का ऐक्सेस, सिर्फ़ उन संसाधनों तक सीमित होता है जो सर्वर की ज़रूरतें पूरी करता है. अच्छी तरह से बताए गए एडमिन टास्क को पूरा करने के लिए इस लेवल का इस्तेमाल करें ज़रूरी है. उदाहरण के लिए, खास जानकारी देने वाला ऐसा जॉब चलाते समय जो पूरे डेटा को पढ़ता है पूरे डेटाबेस में, फ़ाइल में होने वाले गलती से लिखे जाने से बचा जा सकता है. इसके लिए, रीड-ओनली ऐक्सेस सेट करना ज़रूरी है सुरक्षा के नियम लागू करने होंगे और फिर खास अधिकारों के साथ एडमिन SDK टूल शुरू किया जा रहा है तय करें.

एडमिन के खास अधिकारों की मदद से पुष्टि करें

किसी सेवा के क्रेडेंशियल से Firebase एडमिन SDK टूल का इस्तेमाल शुरू करने पर खाते के साथ आपके Firebase प्रोजेक्ट में एडिटर की भूमिका होनी चाहिए. अपने प्रोजेक्ट के रीयलटाइम डेटाबेस के लिए पढ़ने और लिखने का ऐक्सेस पूरा करें.

Java
// 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) {
  }
});
Node.js
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());
});
Python
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)

सीमित अधिकारों के साथ पुष्टि करें

सबसे सही तरीका यह है कि किसी सेवा के पास सिर्फ़ उन संसाधनों का ऐक्सेस हो जिनकी उसे ज़रूरत है. पाने के लिए उन संसाधनों पर ज़्यादा बेहतर कंट्रोल, जिन्हें Firebase ऐप्लिकेशन इंस्टेंस कर सकता है ऐक्सेस करने के लिए, अपने सुरक्षा नियमों में यूनीक आइडेंटिफ़ायर का इस्तेमाल करें आपकी सेवा को प्रदर्शित किया जा सकता है. इसके बाद, ऐसे सही नियम तय करें जो आपको सेवा का ऐक्सेस दें सभी संसाधनों को उपलब्ध कराना है. उदाहरण के लिए:

{
  "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'"
    }
  }
}

इसके बाद, अपने सर्वर पर Firebase ऐप्लिकेशन शुरू करते समय, auth ऑब्जेक्ट को बदलने के लिए, databaseAuthVariableOverride विकल्प का इस्तेमाल किया गया आपके डेटाबेस के नियमों की जांच करता है. इस कस्टम auth ऑब्जेक्ट में, uid फ़ील्ड को वह आइडेंटिफ़ायर जिसका इस्तेमाल, सुरक्षा के नियमों में आपकी सेवा को दिखाने के लिए किया जाता है.

Java
// 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);
    }
});
Node.js
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());
});
Python
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)

कुछ मामलों में, हो सकता है कि आपको एडमिन SDK टूल का इस्तेमाल कम करना पड़े, ताकि क्लाइंट की पुष्टि नहीं हुई है. आप ऐसा मान देकर ऐसा कर सकते हैं: डेटाबेस की पुष्टि करने वाले वैरिएबल को बदलने के लिए null.

Java
// 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);
    }
});
Node.js
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());
});
Python
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)

अगले चरण