Copied example from documentation of Youtube API not working - javascript

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>

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

Related

The gapi.client is not created after loading the Googe API correctly

I'm trying to use the YouTube API to create a small webapp. However, it seems like the gapi.client object is not being created. The code runs on the Google API console but not locally.
I have loaded the JS scripts as follows:
<script src="https://apis.google.com/js/api.js" type="application/javascript"></script>
<script type="text/javascript" src="js/app.js"></script>
In the app.js the loadClient function is as follows:
function loadClient() {
gapi.client.setApiKey(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); });
}
An error shows up when the below line is executed:
gapi.client.setApiKey(API_KEY);
The error ---> TypeError: Cannot read property 'setApiKey' of undefined at loadClient (app.js:7)
I'd like to understand why this is happening and how to solve it. Thanks.

Error with the promise/catch from a Firestore query

I've modified the code below a bit but this is an externally facing endpoint where a mobile client can ping this endpoint and send some pushes to appropriate users.
However, in my console, I'm getting an error:
UnhandledPromiseRejectionWarning: TypeError: assert.isNotOk is not a function
function myFunc(request, response) {
var db = firestore.firestore();
db.collection("myCollection")
.doc(request.params.someParam)
.get()
.then(docSnapshot => {
if (docSnapshot.exists) {
for (var userId of request.params.userIds) {
sendPush(userId, request.params);
continue;
} else {
response.error("Unable to get param");
}
}).catch((error) => {
assert.isNotOk(error, 'Promise error');
done();
});;
});
Any idea what I'm doing wrong here? Thanks
As you can see in the documentation for node's assert, there is no method called isNotOk. However, this is a method called ok. In any event, it's not clear to me what you're trying to do with that line, since you already know at that point that there's an error. Perhaps you just want to log it?

Linkedin Javascript SDK

I am trying to call an API to get user information but its always giving me this error: Could not find person based on: ~, below is my code to call the people API:
IN.API.Raw('/people/~:(id,first-name,last-name,maiden-name,headline,location,industry,picture-url,summary,specialties,positions,picture-urls::(original),email-address,num-connections,site-standard-profile-request,api-standard-profile-request,public-profile-url)?format=json').result((data) => {
callback(data);
}).error((error) => {
console.log('Error:', error);
});

How to authorize in Skype web SDK

Recently preview version of new Skype SDK has been released. So I've downloaded samples, read MSDN articles and tried to write simplest JS script to make login to this SDK. So, I've taken code from this MSDN article and slightly modified it(sample code hasn't been working at all - used wrong variable). Modified code works, but returns error:
"TypeError: Cannot read property '1' of null at https://swx.cdn.skype.com/build2015/v5/SDK-build.js:8982:77 at handle (https://swx.cdn.skype.com/build2015/v5/SDK-build.js:2220:63) at https://swx.cdn.skype.com/build2015/v5/SDK-build.js:698:34".
So my code is below:
$(function () {
'use strict'; // create an instance of the Application object;
// note, that different instances of Application may
// represent different users
var Application
var client;
Skype.initialize({
apiKey: 'SWX-BUILD-SDK',
}, function (api) {
Application = api.application;
client = new Application();
// when the user clicks on the "Sign In" button $('#signin').click(function () {
// start signing in
client.signInManager.signIn({
username: 'login',
password: 'pass'
}).then(
//onSuccess callback
function () {
// when the sign in operation succeeds display the user name
alert('Signed in as ' + application.personsAndGroupsManager.mePerson.displayName());
},
//onFailure callback
function (error) {
// if something goes wrong in either of the steps above,
// display the error message
alert(error || 'Cannot sign in');
});
}, function (err) {
alert('some error occurred: ' + err);
});
});
What am I doing wrong?
Finally I've found right answer: for now Skype Web SDK is only for Skype for Business not Skype for consumers. Sad but true.

Uncaught ReferenceError: init is not defined

im a newbie in javascript and wanted to learn something, I've been wondering, i found this on https://developers.google.com/api-client-library/javascript/features/cors where i can use cors so i can access a google API like kissflow, i dont know if im in the right way. so here's the thing, im using the standalone Auth client that was described in the said site, but everytime i tried to run the program the error prompt
Uncaught ReferenceError: init is not defined
i just copied the code at the site which is
<script src="https://apis.google.com/js/api.js"
type="text/javascript">
</script>`<script type="text/javascript">`
//<![CDATA[gapi.load('auth', init);//]]>
</script>
Try this:
<script src="https://apis.google.com/js/api.js"
type="text/javascript">
</script>
<script>
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: "<YOUR_CLIENT_ID>"});
});
</script>
Make sure that you host this locally, and when creating the client ID, add the localhost URL with the port. Without a URL, the client ID will not send back a proper response, resulting in an error. Since the client ID doesn't support "file:///", you have to use a web hosting service or something, or the easier route, just download the latest python and set up a localhost server.
Latest python download: https://www.python.org/downloads/
Setting up the server: https://developer.mozilla.org/en-US/docs/Learn/Common_questions/set_up_a_local_testing_server#running_a_simple_local_http_server
Note that sometimes you have to use "py" instead of "python" for reasons I am too lazy to research 😊.
To actually initiate the login and use the API:
<script>
function authenticate() {
return gapi.auth2.getAuthInstance().signIn({scope: ""/*Declare scopes with spaces in between, depends on API you're using*/})
.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(""/*Declare discovery document, depends on API you're using*/)
.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.classroom.courses.list({}) //Used classroom courses list as an example, but use the apropriate API Fields, found in your method's "Overview" section on the API Documentation
.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>"});
});
authenticate().then(loadClient).then(execute)
</script>
Note that sometimes you have to clear cache in order for this to work (I mean, worked for me), so if you have some trouble, try that.

Categories

Resources