1

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>
asked Oct 3, 2020 at 3:28
1

1 Answer 1

2

"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.

answered Oct 3, 2020 at 3:32
Sign up to request clarification or add additional context in comments.

4 Comments

Okay now I changed it but now I get setApiKey is undefined, should i use this and option 2? github.com/google/google-api-javascript-client/blob/master/docs/…
You are missing the gapi.load('client', start); line from Option 2.
But loadClient() returns gapi.client.load isn't that the same thing
Now the error is "Cannot read property 'setApiKey' of undefined" - which is just the same root issue that gapi.client is undefined.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.