Skip to content

Latest commit

 

History

History
81 lines (60 loc) · 3.26 KB

File metadata and controls

81 lines (60 loc) · 3.26 KB

YouTube: Get information about a YouTube channel

This quickstart demonstrates how to query the YouTube Data API using Cloud Functions for Firebase with an HTTPS trigger.

Introduction

The function getChannelInfo returns information about a Youtube channel. By default it will return information about the Firebase YouTube channel, but you can pass it a channelId URL Query parameter to query any channel you'd like.

Setup

Get a YouTube API Key

  1. Create a Firebase Project on the Firebase Console if you don't already have a project you want to use.
    1. Upgrade your Firebase project to the Blaze "pay as you go" plan
  2. Enable the Youtube API by visiting the API console, selecting your Firebase project, and clicking "ENABLE".
    1. Once the API is enabled, visit the credentials tab and click "CREATE CREDENTIALS" to create a YouTube API key.

Clone and configure the function

  1. Install the Firebase CLI and log in:
    npm install --global firebase-tools
    
    firebase login
    
  2. Clone or download this repo and open the youtube directory.
  3. cd into the functions directory and install dependencies with npm install
  4. Set up your Firebase project by running firebase use --add with the Firebase CLI, select your Project ID and follow the instructions.
  5. Set the YouTube API key as an environment variable:
    firebase functions:config:set youtube.key="THE API KEY"

Run your function locally with the Firebase Emulator Suite

  1. Set up the Firebase emulators with your config (docs):
    cd functions
    
    firebase functions:config:get > .runtimeconfig.json
  2. Run the following command to start the emulator:
    firebase emulators:start --only functions
  3. Check the emulator output to find the URL of the getChannelInfo function. It will looks something like http://localhost:5001/my-project-id/us-central1/getChannelInfo
  4. Via CURL or in your browser, visit the URL that the function is running at. Optionally, add a query string ?channelId=SOME_CHANNEL_ID to the end of the URL.
  5. You should get a JSON response with information about the YouTube channel!

Deploy the app to prod

Deploy to Firebase using the following command:

firebase deploy

This deploys and activates the getChannelInfo function.

The first time you call firebase deploy on a new project with Functions will take longer than usual.

Modify it to your needs

Now that you've got this sample working, modify it to work for your use case! Some ideas:

  • Check out the other things you can query with the YouTube Data API
  • Convert getChannelInfo function to a scheduled function, and write the new latest videos for a channel into Firestore or Realtime Database
  • ...anything else you can think of!