กำหนดค่าผู้ให้บริการข้อมูลประจำตัว OAuth โดยทางโปรแกรมสำหรับการตรวจสอบสิทธิ์ Firebase

คุณสามารถใช้ Google Cloud Identity Platform REST API เพื่อจัดการการกำหนดค่าผู้ให้บริการข้อมูลประจำตัว (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 ใหม่

หากต้องการเพิ่มการกำหนดค่าผู้ให้บริการข้อมูลประจำตัว OAuth (IdP) ใหม่ ให้โพสต์การกำหนดค่าใหม่ไปยังตำแหน่งข้อมูลของ projects.defaultSupportedIdpConfigs

คุณจะต้องระบุ ID ของผู้ให้บริการข้อมูลประจำตัวและรหัสไคลเอ็นต์ของคุณและความลับของลูกค้า ซึ่งโดยทั่วไปคุณจะได้รับจากไซต์นักพัฒนาซอฟต์แวร์ของผู้ให้บริการ ต่อไปนี้คือผู้ให้บริการข้อมูลประจำตัวที่ Firebase รองรับและรหัส:

ผู้ให้บริการ รหัสประจำตัวประชาชน
แอปเปิล apple.com
แอปเปิ้ลเกมเซ็นเตอร์ gc.apple.com
เฟสบุ๊ค facebook.com
GitHub github.com
Google google.com
เกมของ Google Play playgames.google.com
ลิงค์อิน linkedin.com
ไมโครซอฟต์ microsoft.com
ทวิตเตอร์ twitter.com
ยาฮู 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 ให้กำหนดค่าผู้ให้บริการข้อมูลประจำตัวใหม่ตามที่แสดงใน ส่วนก่อนหน้าแทน