Skip to content

Instantly share code, notes, and snippets.

@amitkhare
Last active June 4, 2020 20:06
Show Gist options
  • Save amitkhare/c28f76c592c59b568a84d33c2312afa8 to your computer and use it in GitHub Desktop.
Save amitkhare/c28f76c592c59b568a84d33c2312afa8 to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://apis.google.com/js/api.js"></script>
<script>
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({
scope: 'https://www.googleapis.com/auth/youtube.force-ssl'
})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
return gapi.client.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
// created stream id -----
function createStreamKey(title) {
return gapi.client.youtube.liveStreams.insert({
"part": [
"snippet",
"cdn"
],
"resource": {
"snippet": {
"title": title, //"live Stream KEY"
},
"cdn": {
"frameRate": "30fps",
"ingestionType": "rtmp",
"resolution": "720p"
}
}
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function getStreamKeyList() {
return gapi.client.youtube.liveStreams.list({
"part": [
"snippet",
"contentDetails"
],
"mine": true
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function getStreamKeyById(StreamKeyId) {
return gapi.client.youtube.liveStreams.list({
"part": [
"snippet",
"contentDetails"
],
"id": [
StreamKeyId
]
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function updateStreamKey(StreamKeyId, Title) {
return gapi.client.youtube.liveStreams.update({
"part": [
"snippet",
"contentDetails",
"cdn",
"status"
],
"resource": {
"id": StreamKeyId,
"snippet": {
"title": Title, //"New Title",
"description": "New Description"
}
}
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
// this will only work when no broadcast bound to it
function deleteStreamKey(StreamKeyId) {
return gapi.client.youtube.liveStreams.delete({
"id": StreamKeyId
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function createBroadcast(title) {
return gapi.client.youtube.liveBroadcasts.insert({
"part": [
"snippet",
"contentDetails",
"status"
],
"resource": {
"snippet": {
"title": title, //"pubG Mobile Test new",
"description": "Test Description",
"scheduledStartTime": "2022-10-01T09:45:00.000+02:00"
},
"status": {
"selfDeclaredMadeForKids": false,
"privacyStatus": "private",
"liveBroadcastPriority": "normal"
},
"contentDetails": {
"enableDvr": true,
"enableAutoStart": true,
"enableAutoStop": true,
"latencyPreference": "normal"
}
}
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function getBroadcastList() {
return gapi.client.youtube.liveBroadcasts.list({
"part": [
"snippet"
],
"broadcastStatus": "upcoming",
"broadcastType": "event"
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function getBroadcastById(BroadcastId) {
return gapi.client.youtube.liveBroadcasts.list({
"part": [
"snippet",
"contentDetails",
"status"
],
"id": [
BroadcastId
]
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function updateBroadcast(BroadcastId, Title) {
return gapi.client.youtube.liveBroadcasts.update({
"part": [
"snippet",
"status"
],
"resource": {
"id": BroadcastId,
"snippet": {
"title": Title, //"Test Updated",
"scheduledStartTime": "2021-10-01T09:45:00.000+02:00"
},
"status": {
"privacyStatus": "private"
}
}
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function deleteBroadcast(BroadcastId) {
return gapi.client.youtube.liveBroadcasts.delete({
"id": BroadcastId //"5CcKDh0twj0"
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function bindBroadcast(BroadcastId, StreamId) {
return gapi.client.youtube.liveBroadcasts.bind({
"id": BroadcastId,
"streamId": StreamId
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: "YOUR_OAUTH_ID"});
});
</script>
</head>
<body>
<button onclick="authenticate().then(loadClient)">authorize</button>
<button onclick="getBroadcastList()">Broadcast List</button>
<button onclick="getBroadcastById('3ss34mKTVEM')">Broadcast By Id</button>
<button onclick="updateBroadcast('3ss34mKTVEM', 'broadcast-titile')">Update Broadcast</button>
<button onclick="createStreamKey('This shhould work key')">Create Stream Key</button>
<button onclick="bindBroadcast('3ss34mKTVEM', '6Arfvm93bfnXl2-O4VS1JQ1591299912245816')">Bind Broadcast</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment