I'm trying to search for a video with some words using Youtube API and I have run my example on this interactive app https://developers.google.com/youtube/v3/docs/search/list?apix=true&apix_params=%7B%22part%22%3A%5B%22snippet%22%5D%2C%22q%22%3A%22ay%20vamos%22%7D. I have copied the source code on that page and removed authorization because I don't need it and filled credentials. This is the error I keep on getting Uncaught TypeError: Cannot read property 'youtube' of undefined
<body>
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for youtube.search.list
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#javascript
*/
function loadClient() {
gapi.client.setApiKey("-------------------");
return gapi.client
.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
.then(
function () {
return gapi.client.youtube.search
.list({
part: ["snippet"],
q: "ay vamos",
})
.then(
function (response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function (err) {
console.error("Execute error", err);
}
);
},
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.
</script>
<button onclick="loadClient()">execute</button>
</body>
-
I solved this problem by adding a tag that I got from this post stackoverflow.com/questions/27492954/…A.K.M. Adib– A.K.M. Adib2020年10月03日 03:48:08 +00:00Commented Oct 3, 2020 at 3:48
1 Answer 1
"Cannot read propery 'youtube' of undefined" hints that gapi.client isn't defined properly. I ran your code myself and printing gapi.client to the console outputs null.
Also, I couldn't find anything that is calling your loadClient() function which looks like it is responsible for actually loading the youtube client? That could be a separate issue.
4 Comments
gapi.load('client', start); line from Option 2.gapi.client is undefined.Explore related questions
See similar questions with these tags.