- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
- Here we use the javascript for explore the youtube data api.but you can use the api in any langauge like java , php , python. youtube data api provide the data in form of the json data format.
- youtube data api provide the10000 request per day it is enough for us.
- we can see the our current the Quotas of our request.
- Description : This code Fetch the all the playlist by channel id. Copy this code in between head tag and modified the code by putting the api key and Channel ID.
See Documantation of the Youtube Data Api .
Fetch the Playlist 👈 :
First Generate the Api Key :Click Here
<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.readonly"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
gapi.client.setApiKey("Enter Your Api key");
//if not generated api yet then Click Here
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.
function execute() {
return gapi.client.youtube.playlists.list({
"part": [
"contentDetails,snippet,id,status,player"
],
"channelId": "Enter A Channel Id here"
})
.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_CLIENT_ID"}); // if do not mentioned any thing
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
Fetch the the video from the playlist 👈 :
<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.readonly"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
gapi.client.setApiKey("Please ENTER YOUR_API_KEY");
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); });
}
function execute() {
return gapi.client.youtube.playlistItems.list({
"part": {
"contentDetails,snippet,status,id"
],
"maxResults": 100, //Here you can write the max result
"playlistId": "fafsfsfs" // here write the playlist id
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
//response has the result
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
Fetch the recent activity 👈 :
<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.readonly"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
gapi.client.setApiKey("YOUR_API_KEY");
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.
function execute() {
return gapi.client.youtube.activities.list({
"part": [
"contentDetails,id,snippet"
],
"channelId": "dwsd", Enter the channel Id
"maxResults": 100 //write the number of results
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},//Response give the results
function(err) { console.error("Execute error", err); });
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
Comments
Continue with videos and playlist...
ReplyDeleteNice code
Deleteplease share the combined code
ReplyDelete