Admin SDK คือชุดของไลบรารีเซิร์ฟเวอร์ที่ให้คุณโต้ตอบกับ Firebase จากสภาพแวดล้อมที่มีสิทธิพิเศษเพื่อดำเนินการต่างๆเช่น:
- อ่านและเขียนข้อมูลฐานข้อมูลแบบเรียลไทม์ด้วยสิทธิ์ผู้ดูแลระบบแบบเต็ม
- ส่งข้อความ Firebase Cloud Messaging แบบเป็นโปรแกรมโดยใช้วิธีง่ายๆทางเลือกอื่นสำหรับโปรโตคอลเซิร์ฟเวอร์ Firebase Cloud Messaging
- สร้างและตรวจสอบโทเค็นการตรวจสอบสิทธิ์ Firebase
- เข้าถึงทรัพยากร Google Cloud เช่นที่เก็บข้อมูล Cloud Storage และฐานข้อมูล Cloud Firestore ที่เชื่อมโยงกับโครงการ Firebase ของคุณ
- สร้างคอนโซลผู้ดูแลระบบที่เรียบง่ายของคุณเองเพื่อทำสิ่งต่างๆเช่นค้นหาข้อมูลผู้ใช้หรือเปลี่ยนที่อยู่อีเมลของผู้ใช้สำหรับการตรวจสอบสิทธิ์
หากคุณสนใจที่จะใช้ Node.js SDK เป็นไคลเอนต์สำหรับการเข้าถึงของผู้ใช้ปลายทาง (ตัวอย่างเช่นในเดสก์ท็อป Node.js หรือแอปพลิเคชัน IoT) ซึ่งต่างจากการเข้าถึงของผู้ดูแลระบบจากสภาพแวดล้อมที่มีสิทธิพิเศษ (เช่นเซิร์ฟเวอร์) คุณ ควรปฏิบัติตาม คำแนะนำในการตั้งค่าไคลเอ็นต์ JavaScript SDK แทน
นี่คือเมทริกซ์คุณลักษณะที่แสดงว่าคุณลักษณะของ Firebase ใดบ้างที่รองรับในแต่ละภาษา:
หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับการรวม Admin SDK สำหรับการใช้งานเหล่านี้โปรดดูเอกสารคู่มือ Realtime Database , FCM , Authentication , Remote Config และ Cloud Storage ที่ เกี่ยวข้อง ส่วนที่เหลือของหน้านี้จะเน้นไปที่การตั้งค่าพื้นฐานสำหรับ Admin SDK
ข้อกำหนดเบื้องต้น
ตรวจสอบให้แน่ใจว่าคุณมีแอปเซิร์ฟเวอร์
ตรวจสอบให้แน่ใจว่าเซิร์ฟเวอร์ของคุณเรียกใช้สิ่งต่อไปนี้โดยขึ้นอยู่กับ Admin SDK ที่คุณใช้:
- Admin Node.js SDK - Node.js 10.10.0+
- Admin Java SDK - Java 7+ (แนะนำ Java 8+)
เลิกรองรับ Java 7 แล้ว - Admin Python SDK - Python 3.5+ (แนะนำ Python 3.6+)
- Admin Go SDK - Go 1.11+
- Admin .NET SDK - .NET Framework 4.5+ หรือ. Net Core 1.5+
ตั้งค่าโครงการ Firebase และบัญชีบริการ
ในการใช้ Firebase Admin SDK คุณจะต้องมีสิ่งต่อไปนี้:
- โครงการ Firebase
- บัญชีบริการสำหรับสื่อสารกับ Firebase
- ไฟล์การกำหนดค่าที่มีข้อมูลรับรองบัญชีบริการของคุณ
หากคุณยังไม่มีโปรเจ็กต์ Firebase คุณต้องสร้างโปรเจ็กต์ใน คอนโซล Firebase ไปที่ ทำความเข้าใจกับโครงการ Firebase เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับโครงการ Firebase
เพิ่ม SDK
หากคุณกำลังตั้งค่าโปรเจ็กต์ใหม่คุณต้องติดตั้ง SDK สำหรับภาษาที่คุณเลือก
โหนด js
Firebase Admin Node.js SDK พร้อมใช้งานใน npm หากคุณยังไม่มีไฟล์ package.json
ให้สร้างไฟล์ผ่าน npm init
จากนั้นติดตั้งแพ็คเกจ firebase firebase-admin
npm และบันทึกลงใน package.json
ของคุณ:
$ npm install firebase-admin --save
ในการใช้โมดูลในแอปพลิเคชันของคุณ require
จากไฟล์ JavaScript ใด ๆ :
var admin = require('firebase-admin');
หากคุณใช้ ES2015 คุณสามารถ import
โมดูลแทนได้:
import * as admin from 'firebase-admin';
Java
Firebase Admin Java SDK ถูกเผยแพร่ไปยังที่เก็บกลาง Maven ในการติดตั้งไลบรารีให้ประกาศว่าเป็นการอ้างอิงในไฟล์ build.gradle
ของคุณ:
dependencies {
implementation 'com.google.firebase:firebase-admin:7.1.1'
}
หากคุณใช้ Maven เพื่อสร้างแอปพลิเคชันของคุณคุณสามารถเพิ่มการอ้างอิงต่อไปนี้ใน pom.xml
ของคุณ:
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>7.1.1</version>
</dependency>
Python
Firebase Admin Python SDK พร้อมใช้งานผ่าน pip คุณสามารถติดตั้งไลบรารีสำหรับผู้ใช้ทั้งหมดผ่าน sudo
:
$ sudo pip install firebase-admin
หรือคุณสามารถติดตั้งไลบรารีสำหรับผู้ใช้ปัจจุบันโดยส่งแฟ --user
:
$ pip install --user firebase-admin
ไป
สามารถติดตั้ง Go Admin SDK ได้โดยใช้ยูทิลิตี้ go get
:
# Install as a module dependency
$ go get firebase.google.com/go/v4
# Install to $GOPATH
$ go get firebase.google.com/go
ค#
สามารถติดตั้ง. NET Admin SDK ได้โดยใช้ตัวจัดการแพ็คเกจ. NET:
$ Install-Package FirebaseAdmin -Version 2.1.0
หรือติดตั้งโดยใช้ยูทิลิตี้บรรทัดคำสั่ง dotnet
:
$ dotnet add package FirebaseAdmin --version 2.1.0
หรือคุณสามารถติดตั้งได้โดยเพิ่มรายการอ้างอิงแพ็คเกจต่อไปนี้ในไฟล์ .csproj
ของคุณ:
<ItemGroup>
<PackageReference Include="FirebaseAdmin" Version="2.1.0" />
</ItemGroup>
เริ่มต้น SDK
เมื่อคุณสร้างโครงการ Firebase แล้วคุณสามารถเริ่มต้น SDK ด้วยกลยุทธ์การให้สิทธิ์ที่รวมไฟล์บัญชีบริการของคุณเข้ากับ ข้อมูลรับรองเริ่มต้นของแอปพลิเคชัน Google
โครงการ Firebase รองรับ บัญชีบริการ ของ Google ซึ่งคุณสามารถใช้เพื่อเรียก API เซิร์ฟเวอร์ Firebase จากเซิร์ฟเวอร์แอปหรือสภาพแวดล้อมที่เชื่อถือได้ หากคุณกำลังพัฒนาโค้ดในเครื่องหรือปรับใช้แอปพลิเคชันในองค์กรคุณสามารถใช้ข้อมูลรับรองที่ได้รับผ่านบัญชีบริการนี้เพื่ออนุญาตคำขอของเซิร์ฟเวอร์
ในการตรวจสอบสิทธิ์บัญชีบริการและอนุญาตให้เข้าถึงบริการ Firebase คุณต้องสร้างไฟล์คีย์ส่วนตัวในรูปแบบ JSON
ในการสร้างไฟล์คีย์ส่วนตัวสำหรับบัญชีบริการของคุณ:
ในคอนโซล Firebase เปิด การตั้งค่า> บัญชีบริการ
คลิก สร้างคีย์ส่วนตัวใหม่ จากนั้นยืนยันโดยคลิก สร้างคีย์
จัดเก็บไฟล์ JSON ที่มีคีย์อย่างปลอดภัย
เมื่อทำการอนุมัติผ่านบัญชีบริการคุณมีสองทางเลือกในการระบุข้อมูลรับรองให้กับแอปพลิเคชันของคุณ คุณสามารถตั้งค่าตัวแปรสภาพแวดล้อม GOOGLE_APPLICATION_CREDENTIALS หรือจะส่งเส้นทางไปยังรหัสบัญชีบริการอย่างชัดเจนก็ได้ ตัวเลือกแรกมีความปลอดภัยมากกว่าและขอแนะนำอย่างยิ่ง
ในการตั้งค่าตัวแปรสภาพแวดล้อม:
ตั้งค่าตัวแปรสภาพแวดล้อม GOOGLE_APPLICATION_CREDENTIALS เป็นเส้นทางไฟล์ของไฟล์ JSON ที่มีคีย์บัญชีบริการของคุณ ตัวแปรนี้ใช้กับเซสชันเชลล์ปัจจุบันของคุณเท่านั้นดังนั้นหากคุณเปิดเซสชันใหม่ให้ตั้งค่าตัวแปรอีกครั้ง
Linux หรือ macOS
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
Windows
ด้วย PowerShell:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
หลังจากที่คุณทำตามขั้นตอนข้างต้นเรียบร้อยแล้ว Application Default Credentials (ADC) จะสามารถระบุข้อมูลรับรองของคุณโดยปริยายทำให้คุณสามารถใช้ข้อมูลรับรองบัญชีบริการเมื่อทำการทดสอบหรือทำงานในสภาพแวดล้อมที่ไม่ใช่ของ Google
เริ่มต้น SDK ดังที่แสดง:
โหนด js
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});
Java
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
Python
default_app = firebase_admin.initialize_app()
ไป
app, err := firebase.NewApp(context.Background(), nil)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
ค#
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.GetApplicationDefault(),
});
ใช้โทเค็นการรีเฟรช OAuth 2.0
นอกจากนี้ Admin SDK ยังมีข้อมูลรับรองซึ่งช่วยให้คุณตรวจสอบสิทธิ์ด้วยโทเค็นการรีเฟรช Google OAuth2 :
โหนด js
var refreshToken; // Get refresh token from OAuth2 flow
admin.initializeApp({
credential: admin.credential.refreshToken(refreshToken),
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});
Java
FileInputStream refreshToken = new FileInputStream("path/to/refreshToken.json");
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(refreshToken))
.setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
Python
cred = credentials.RefreshToken('path/to/refreshToken.json')
default_app = firebase_admin.initialize_app(cred)
ไป
opt := option.WithCredentialsFile("path/to/refreshToken.json")
config := &firebase.Config{ProjectID: "my-project-id"}
app, err := firebase.NewApp(context.Background(), config, opt)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
ค#
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile("path/to/refreshToken.json"),
});
เริ่มต้นโดยไม่มีพารามิเตอร์
SDK ยังสามารถเริ่มต้นได้โดยไม่มีพารามิเตอร์ ในกรณีนี้ SDK จะใช้ ข้อมูลรับรองเริ่มต้นของแอปพลิเคชัน Google เนื่องจากการค้นหาข้อมูลรับรองเริ่มต้นเป็นไปโดยอัตโนมัติในสภาพแวดล้อมของ Google โดยไม่จำเป็นต้องจัดหาตัวแปรสภาพแวดล้อมหรือการกำหนดค่าอื่น ๆ วิธีนี้จึงแนะนำให้ใช้ SDK แบบ intializing สำหรับแอปพลิเคชันที่ทำงานบน Compute Engine, Kubernetes Engine, App Engine และ Cloud Functions
หากต้องการระบุตัวเลือกการเริ่มต้นสำหรับบริการเช่น Realtime Database, Cloud Storage หรือ Cloud Functions ให้ใช้ตัวแปรสภาพแวดล้อม FIREBASE_CONFIG
หากเนื้อหาของตัวแปร FIREBASE_CONFIG
ขึ้นต้นด้วย {
มันจะถูกแยกวิเคราะห์เป็นออบเจ็กต์ JSON มิฉะนั้น SDK จะถือว่าสตริงเป็นเส้นทางของไฟล์ JSON ที่มีตัวเลือก
โหนด js
// Initialize the default app
var admin = require('firebase-admin');
var app = admin.initializeApp();
Java
FirebaseApp.initializeApp();
Python
default_app = firebase_admin.initialize_app()
ไป
app, err := firebase.NewApp(context.Background(), nil)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
ค#
FirebaseApp.Create();
เมื่อเริ่มต้นแล้วคุณสามารถใช้ Admin SDK เพื่อทำงานประเภทต่อไปนี้ให้สำเร็จ:
- ใช้การรับรองความถูกต้องแบบกำหนดเอง
- จัดการผู้ใช้การตรวจสอบสิทธิ์ Firebase ของคุณ
- อ่านและเขียนข้อมูลจากฐานข้อมูลเรียลไทม์
- ส่งข้อความ Firebase Cloud Messaging
เริ่มต้นแอพหลายตัว
ในกรณีส่วนใหญ่คุณต้องเริ่มต้นแอปเริ่มต้นเพียงแอปเดียวเท่านั้น คุณสามารถเข้าถึงบริการจากแอพนั้นได้สองวิธีที่เทียบเท่ากัน:
โหนด js
// Initialize the default app
var defaultApp = admin.initializeApp(defaultAppConfig);
console.log(defaultApp.name); // '[DEFAULT]'
// Retrieve services via the defaultApp variable...
var defaultAuth = defaultApp.auth();
var defaultDatabase = defaultApp.database();
// ... or use the equivalent shorthand notation
defaultAuth = admin.auth();
defaultDatabase = admin.database();
Java
// Initialize the default app
FirebaseApp defaultApp = FirebaseApp.initializeApp(defaultOptions);
System.out.println(defaultApp.getName()); // "[DEFAULT]"
// Retrieve services by passing the defaultApp variable...
FirebaseAuth defaultAuth = FirebaseAuth.getInstance(defaultApp);
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance(defaultApp);
// ... or use the equivalent shorthand notation
defaultAuth = FirebaseAuth.getInstance();
defaultDatabase = FirebaseDatabase.getInstance();
Python
# Import the Firebase service
from firebase_admin import auth
# Initialize the default app
default_app = firebase_admin.initialize_app(cred)
print(default_app.name) # "[DEFAULT]"
# Retrieve services via the auth package...
# auth.create_custom_token(...)
ไป
// Initialize default app
app, err := firebase.NewApp(context.Background(), nil)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
// Access auth service from the default app
client, err := app.Auth(context.Background())
if err != nil {
log.Fatalf("error getting Auth client: %v\n", err)
}
ค#
// Initialize the default app
var defaultApp = FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.GetApplicationDefault(),
});
Console.WriteLine(defaultApp.Name); // "[DEFAULT]"
// Retrieve services by passing the defaultApp variable...
var defaultAuth = FirebaseAuth.GetAuth(defaultApp);
// ... or use the equivalent shorthand notation
defaultAuth = FirebaseAuth.DefaultInstance;
การใช้งานบางกรณีต้องการให้คุณสร้างหลายแอพพร้อมกัน ตัวอย่างเช่นคุณอาจต้องการอ่านข้อมูลจากฐานข้อมูลเรียลไทม์ของโปรเจ็กต์ Firebase หนึ่งโปรเจ็กต์และสร้างโทเค็นที่กำหนดเองสำหรับโปรเจ็กต์อื่น หรือคุณอาจต้องการตรวจสอบสิทธิ์สองแอปด้วยข้อมูลรับรองแยกกัน Firebase SDK ช่วยให้คุณสร้างแอปได้หลายแอปพร้อมกันโดยแต่ละแอปจะมีข้อมูลการกำหนดค่าของตนเอง
โหนด js
// Initialize the default app
admin.initializeApp(defaultAppConfig);
// Initialize another app with a different config
var otherApp = admin.initializeApp(otherAppConfig, 'other');
console.log(admin.app().name); // '[DEFAULT]'
console.log(otherApp.name); // 'other'
// Use the shorthand notation to retrieve the default app's services
var defaultAuth = admin.auth();
var defaultDatabase = admin.database();
// Use the otherApp variable to retrieve the other app's services
var otherAuth = otherApp.auth();
var otherDatabase = otherApp.database();
Java
// Initialize the default app
FirebaseApp defaultApp = FirebaseApp.initializeApp(defaultOptions);
// Initialize another app with a different config
FirebaseApp otherApp = FirebaseApp.initializeApp(otherAppConfig, "other");
System.out.println(defaultApp.getName()); // "[DEFAULT]"
System.out.println(otherApp.getName()); // "other"
// Use the shorthand notation to retrieve the default app's services
FirebaseAuth defaultAuth = FirebaseAuth.getInstance();
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();
// Use the otherApp variable to retrieve the other app's services
FirebaseAuth otherAuth = FirebaseAuth.getInstance(otherApp);
FirebaseDatabase otherDatabase = FirebaseDatabase.getInstance(otherApp);
Python
# Initialize the default app
default_app = firebase_admin.initialize_app(cred)
# Initialize another app with a different config
other_app = firebase_admin.initialize_app(cred, name='other')
print(default_app.name) # "[DEFAULT]"
print(other_app.name) # "other"
# Retrieve default services via the auth package...
# auth.create_custom_token(...)
# Use the `app` argument to retrieve the other app's services
# auth.create_custom_token(..., app=other_app)
ไป
// Initialize the default app
defaultApp, err := firebase.NewApp(context.Background(), nil)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
// Initialize another app with a different config
opt := option.WithCredentialsFile("service-account-other.json")
otherApp, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
// Access Auth service from default app
defaultClient, err := defaultApp.Auth(context.Background())
if err != nil {
log.Fatalf("error getting Auth client: %v\n", err)
}
// Access auth service from other app
otherClient, err := otherApp.Auth(context.Background())
if err != nil {
log.Fatalf("error getting Auth client: %v\n", err)
}
ค#
// Initialize the default app
var defaultApp = FirebaseApp.Create(defaultOptions);
// Initialize another app with a different config
var otherApp = FirebaseApp.Create(otherAppConfig, "other");
Console.WriteLine(defaultApp.Name); // "[DEFAULT]"
Console.WriteLine(otherApp.Name); // "other"
// Use the shorthand notation to retrieve the default app's services
var defaultAuth = FirebaseAuth.DefaultInstance;
// Use the otherApp variable to retrieve the other app's services
var otherAuth = FirebaseAuth.GetAuth(otherApp);
กำหนดขอบเขตสำหรับฐานข้อมูลแบบเรียลไทม์และการพิสูจน์ตัวตน
หากคุณใช้ Google Compute Engine VM ที่มีข้อมูลประจำตัวเริ่มต้นของแอปพลิเคชัน Google สำหรับฐานข้อมูลแบบเรียลไทม์หรือการตรวจสอบสิทธิ์ตรวจสอบให้แน่ใจว่าได้ตั้งค่า ขอบเขตการเข้าถึงที่ ถูกต้องด้วย สำหรับฐานข้อมูลแบบเรียลไทม์และการพิสูจน์ตัวตนคุณต้องมีขอบเขตที่ลงท้ายด้วย userinfo.email
และ cloud-platform
หรือ firebase.database
ในการตรวจสอบขอบเขตการเข้าถึงที่มีอยู่และเปลี่ยนแปลงให้รันคำสั่งต่อไปนี้โดยใช้ gcloud
gcloud
# Check the existing access scopes
gcloud compute instances describe [INSTANCE_NAME] --format json
# The above command returns the service account information. For example:
"serviceAccounts": [
{
"email": "your.gserviceaccount.com",
"scopes": [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/userinfo.email"
]
}
],
# Stop the VM, then run the following command, using the service account
# that gcloud returned when you checked the scopes.
gcloud compute instances set-service-account [INSTANCE_NAME] --service-account "your.gserviceaccount.com" --scopes "https://www.googleapis.com/auth/firebase.database,https://www.googleapis.com/auth/userinfo.email"
ขั้นตอนถัดไป
เรียนรู้เกี่ยวกับ Firebase:
สำรวจ แอป Firebase ตัวอย่าง
สำรวจรหัสโอเพนซอร์สใน GitHub สำหรับ Node.js , Java และ Python
อ่าน บล็อกโพสต์ที่เกี่ยวข้องกับ Admin SDK โดยหนึ่งในผู้สร้าง Admin SDK ตัวอย่างเช่น การเข้าถึง Firestore และ Firebase ผ่านพร็อกซีเซิร์ฟเวอร์
เพิ่มคุณสมบัติ Firebase ให้กับแอปของคุณ:
- เขียนแบ็กเอนด์แบบไร้เซิร์ฟเวอร์ด้วย Cloud Functions
- จัดเก็บข้อมูลด้วย Realtime Database หรือข้อมูลหยดด้วย Cloud Storage
- รับการแจ้งเตือนด้วย Cloud Messaging