Monday, November 18, 2019

Automated Scheduling of Facebook Live Video via Google Apps Script

Currently Facebook allows users to schedule a live video broadcast only a week in advance. If you broadcast weekly this can get tedious, so with a little bit of coding we can automate the process.

This will require a Facebook account (with admin rights to a page if you broadcast through that page) as well as a Google account. We are going to create a Google Spreadsheet with an associated Google Apps Script that will run every week to schedule your live video.
  1. Create a Google Spreadsheet
  2. Under the Tools menu, click Script editor
  3. Into that new script paste the contents of this file
  4. Create a new Facebook App under the My Apps menu on developers.facebook.com
  5. Under Settings click on Basic and copy your App ID and App Secret (click Show) into the appropriate places back in your Google Script editor
  6. Setup gsuitedevs' OAuth2 library for Google Apps Script by following the four steps there
  7. Run the logRedirectUri function in your Google Script, then under the View menu click Logs, copy the Redirect URL (starting with http)
  8. On the Facebook developer page, set up Facebook Login for your app
  9. Under the Facebook Login Settings, paste in the OAuth Redirect URI that you copied earlier
  10. In your Google Script, run the scheduleLivestream function, this won't actually schedule anything yet but instead will log a URL for you to paste into your browser to authorize the script (under the View menu click Logs, or look at the original spreadsheet)
  11. Using the Facebook Graph API Explorer, add the following permissions to your app: pages_show_list, manage_pages, publish_pages, publish_video
  12. Run the Google Script function scheduleLivestream again and watch for any errors, if it successfully schedules a broadcast then you'll see a new line in your spreadsheet
  13. Add triggers to your Google Script: under the Edit menu click Current project's triggers
    1. Click Add Trigger and set the options:
      1. scheduleLivestream
      2. Head
      3. Time-driven
      4. Week timer
      5. Every Wednesday (or whatever you prefer)
      6. 1pm to 2pm (or whatever)
      7. then click Save
    2. Click Add Trigger and set the options:
      1. onOpen
      2. Head
      3. From spreadsheet
      4. On open
      5. then click Save
  14. In your Google Script, change the following lines:
    1. var status = 'SCHEDULED_UNPUBLISHED';  to  var status = 'SCHEDULED_LIVE';
    2. var day = dateNow.getDate() + ((7dateNow.getDay()) % 7);  to var day = dateNow.getDate() + ((9dateNow.getDay()) % 7);  // for Tuesday
    3. var newDate = new Date(year, month, day, 10, 25); to the time you'd like to start the broadcast
    4. you can also change the var title line to whatever you'd like
  15. Run the scheduleLivestream function again to test, and to make sure everything has been authorized
  16. Check at some point after the Trigger time you set to make sure that the broadcast has been scheduled
Hopefully all of that worked, let me know if I've missed any steps or if this needs more detail.

No comments: