Skip to content

Instantly share code, notes, and snippets.

@irfanfadilah
Created January 21, 2016 04:48
Show Gist options
  • Save irfanfadilah/7286e8e5bcd65a464a2e to your computer and use it in GitHub Desktop.
Save irfanfadilah/7286e8e5bcd65a464a2e to your computer and use it in GitHub Desktop.
YouTube API Snippet for Live Event (Broadcast and Stream)
# Get Access Token
refresh_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token = JSON.parse(
RestClient.post("https://accounts.google.com/o/oauth2/token", {
client_id: Rails.application.secrets.google_client_id,
client_secret: Rails.application.secrets.google_client_secret,
refresh_token: refresh_token, grant_type: "refresh_token"
})
)["access_token"]
# Insert New Broadcast
start_time = "2016-01-14T16:00:00Z"
end_time = "2016-01-14T18:00:00Z"
title = "This Event Created from API"
url = "https://www.googleapis.com/youtube/v3/liveBroadcasts"
url += "?part=id,snippet,contentDetails,status"
url += "&access_token=#{access_token}"
body = {
snippet: {
title: title,
scheduledStartTime: start_time,
scheduledEndTime: end_time
},
status: { privacyStatus: "public" }
}
respond = RestClient.post(url, body.to_json, content_type: :json)
# Insert New Stream
title = "On the Way to Smile"
url = "https://www.googleapis.com/youtube/v3/liveStreams"
url += "?part=id,snippet,cdn,status&mine=true"
url += "&access_token=#{access_token}"
body = { snippet: { title: title }, cdn: { format: "720p", ingestionType: "rtmp" } }
respond = RestClient.post(url, body.to_json, content_type: :json)
# Bind Stream to Broadcast
broadcast_id = "xxxxxxxxxxx"
stream_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
url = "https://www.googleapis.com/youtube/v3/liveBroadcasts/bind"
url += "?part=id,snippet,contentDetails,status"
url += "&id=#{broadcast_id}&streamId=#{stream_id}"
url += "&access_token=#{access_token}"
respond = RestClient.post(url, nil, content_type: :json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment