คุณสามารถใช้ REST API ของ Google Cloud Identity Platform เพื่อจัดการการกำหนดค่าผู้ให้บริการข้อมูลประจำตัว (IdP) OAuth ของโปรเจ็กต์ Firebase แบบเป็นโปรแกรมได้ API นี้ช่วยให้คุณกำหนดค่าผู้ให้บริการข้อมูลประจำตัวที่ต้องการรองรับ รวมถึงอัปเดต เปิดใช้ และปิดใช้การกำหนดค่า OAuth ปัจจุบันของโปรเจ็กต์ได้
รับการให้สิทธิ์
ก่อนที่จะเรียกใช้ REST API ได้ คุณต้องมีโทเค็นเพื่อการเข้าถึง OAuth 2.0 ที่ให้สิทธิ์ผู้แก้ไขแก่โปรเจ็กต์ Firebase ตัวอย่างเช่น หากต้องการรับโทเค็นเพื่อการเข้าถึงโดยใช้บัญชีบริการใน Node.js ให้ทำดังนี้
const googleAuth = require('google-auth-library');
const SCOPES = ['https://www.googleapis.com/auth/cloud-platform'];
async function getAccessToken() {
const serviceAccount = require('/path/to/service_account_key.json');
const jwtClient = new googleAuth.JWT(
serviceAccount.client_email,
null,
serviceAccount.private_key,
SCOPES,
null
);
return jwtClient.authorize().then((tokens) => tokens.access_token);
}
เพิ่มการกำหนดค่าผู้ให้บริการข้อมูลประจำตัว OAuth ใหม่
หากต้องการเพิ่มการกำหนดค่าผู้ให้บริการข้อมูลประจำตัว (IdP) ของ OAuth ใหม่ ให้ POST การกำหนดค่าใหม่ไปยังปลายทาง projects.defaultSupportedIdpConfigs
คุณจะต้องระบุรหัสของผู้ให้บริการข้อมูลประจำตัว รหัสไคลเอ็นต์ และรหัสลับไคลเอ็นต์ ซึ่งโดยปกติแล้วคุณจะได้รับจากเว็บไซต์นักพัฒนาซอฟต์แวร์ของผู้ให้บริการ ผู้ให้บริการข้อมูลประจำตัวที่ Firebase รองรับและรหัสของผู้ให้บริการมีดังนี้
| ผู้ให้บริการ | รหัส IdP |
|---|---|
| Apple | apple.com |
| Apple Game Center | gc.apple.com |
facebook.com |
|
| GitHub | github.com |
google.com |
|
| Google Play Games | playgames.google.com |
linkedin.com |
|
| Microsoft | microsoft.com |
twitter.com |
|
| Yahoo | yahoo.com |
ตัวอย่างเช่น การใช้ Node.js
const fetch = require('node-fetch');
const GCIP_API_BASE = 'https://identitytoolkit.googleapis.com/v2';
async function addIdpConfig(projectId, accessToken, idpId, clientId, clientSecret) {
const uri = `${GCIP_API_BASE}/projects/${projectId}/defaultSupportedIdpConfigs?idpId=${idpId}`;
const options = {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify({
name: `projects/${projectId}/defaultSupportedIdpConfigs/${idpId}`,
enabled: true,
clientId: clientId,
clientSecret: clientSecret,
}),
};
return fetch(uri, options).then((response) => {
if (response.ok) {
return response.json();
} else if (response.status == 409) {
throw new Error('IdP configuration already exists. Update it instead.');
} else {
throw new Error('Server error.');
}
});
}
(async () => {
const projectId = 'your-firebase-project-id';
const accessToken = await getAccessToken();
const idpId = 'facebook.com';
const clientId = 'your-facebook-client-id';
const clientSecret = 'your-facebook-client-secret';
try {
await addIdpConfig(projectId, accessToken, idpId, clientId, clientSecret);
} catch (err) {
console.error(err.message);
}
})().catch(console.error);
หากการเรียกใช้สำเร็จ ระบบจะแสดงผลการกำหนดค่าที่สร้างขึ้นใหม่ เช่น
{
name: 'projects/your-numerical-project-id/defaultSupportedIdpConfigs/facebook.com',
enabled: true,
clientId: 'your-facebook-client-id',
clientSecret: 'your-facebook-client-secret'
}
หากคุณพยายามกำหนดค่าผู้ให้บริการข้อมูลประจำตัวที่กำหนดค่าไว้แล้วสำหรับโปรเจ็กต์ การเรียกจะแสดงผลข้อผิดพลาดของ HTTP 409 ในกรณีนี้ คุณสามารถอัปเดตการกำหนดค่าแทนได้ตามที่อธิบายไว้ด้านล่าง
อัปเดตการกำหนดค่าผู้ให้บริการข้อมูลประจำตัว OAuth
หากต้องการเปิดหรือปิดใช้ผู้ให้บริการข้อมูลประจำตัว OAuth หรืออัปเดตการกำหนดค่าไคลเอ็นต์ของโปรเจ็กต์ ก่อนอื่นให้ดูการกำหนดค่าปัจจุบันของผู้ให้บริการโดยส่งคำขอ GET ไปยังปลายทาง projects.defaultSupportedIdpConfigs
จากนั้นทำการเปลี่ยนแปลงที่ต้องการในการกำหนดค่า และ PATCH การกำหนดค่าใหม่ไปยังปลายทาง projects.defaultSupportedIdpConfigs
ตัวอย่างเช่น การใช้ Node.js
async function getIdpCfg(projectId, accessToken, idpId) {
const uri = `${GCIP_API_BASE}/projects/${projectId}/defaultSupportedIdpConfigs/${idpId}`;
const options = {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`
},
};
return fetch(uri, options).then((response) => {
if (response.ok) {
return response.json();
} else if (response.status == 404) {
throw new Error('IdP configuration not found. First add the IdP'
+ ' configuration to your project.');
} else {
throw new Error('Server error.');
}
});
}
async function updateIdpConfig(accessToken, idpCfg) {
const uri = `${GCIP_API_BASE}/${idpCfg.name}`;
const options = {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify(idpCfg),
};
return fetch(uri, options).then((response) => {
if (response.ok) {
return response.json();
} else if (response.status == 404) {
throw new Error('IdP configuration not found. First add the IdP'
+ ' configuration to your project.');
} else {
throw new Error('Server error.');
}
});
}
(async () => {
const projectId = 'your-firebase-project-id';
const accessToken = await getAccessToken();
const idpId = 'facebook.com';
try {
// Get the IdP's current configuration.
const idpCfg = await getIdpCfg(projectId, accessToken, idpId);
// Update the configuration. (For example, disable the IdP.)
idpCfg.enabled = false;
await updateIdpConfig(accessToken, idpCfg);
} catch (err) {
console.error(err.message);
}
})().catch(console.error);
หากคุณพยายามอัปเดตการกำหนดค่าของผู้ให้บริการข้อมูลประจำตัวที่คุณไม่เคยกำหนดค่าสำหรับโปรเจ็กต์ การเรียกจะแสดงข้อผิดพลาด HTTP 404 แต่ให้กำหนดค่าผู้ให้บริการข้อมูลประจำตัวรายใหม่ตามที่แสดงในส่วนก่อนหน้าแทน