Получение идентификатора канала YouTube из входа в Google с помощью Firebase: методы и пример кода

Чтобы получить идентификатор канала YouTube из входа в Google с помощью Firebase, вы можете использовать клиентскую библиотеку Google API вместе с SDK для аутентификации Firebase и Firebase Admin. Вот пример того, как этого можно добиться с помощью Node.js:

const { google } = require('googleapis');
const admin = require('firebase-admin');
// Initialize Firebase Admin SDK
admin.initializeApp();
// Get the Firebase user's ID token
const userIdToken = '...'; // Replace with the actual user's ID token
// Verify the ID token and extract the user's UID
admin.auth().verifyIdToken(userIdToken)
  .then((decodedToken) => {
    const uid = decodedToken.uid;

    // Retrieve the user's YouTube-linked Google account from Firebase
    admin.auth().getUser(uid)
      .then((userRecord) => {
        const providerData = userRecord.providerData;

        // Find the provider that corresponds to Google
        const googleProvider = providerData.find((provider) => provider.providerId === 'google.com');

        if (googleProvider) {
          // Extract the Google user's access token
          const accessToken = googleProvider.accessToken;

          // Initialize the Google API client
          const youtube = google.youtube({
            version: 'v3',
            auth: accessToken
          });

          // Retrieve the user's YouTube channels
          youtube.channels.list({
            mine: true,
            part: 'id'
          })
            .then((response) => {
              const channel = response.data.items[0];
              const channelId = channel.id;

              console.log('YouTube Channel ID:', channelId);
            })
            .catch((error) => {
              console.error('Error retrieving YouTube channels:', error);
            });
        } else {
          console.error('User does not have a Google-linked account.');
        }
      })
      .catch((error) => {
        console.error('Error retrieving user record from Firebase:', error);
      });
  })
  .catch((error) => {
    console.error('Error verifying ID token:', error);
  });

Обратите внимание, что в этом коде предполагается, что вы уже настроили Firebase Admin SDK и имеете необходимые учетные данные API для своего проекта Google.