Jake Alsemgeest
Jake Alsemgeest

Reputation: 702

YouTube Search Node.js with Google API

I am trying to do a YouTube search with the Google APIs within Node.

I am using this as somewhat of a tutorial:

https://github.com/google/google-api-nodejs-client/#google-apis-nodejs-client

I have some of the basics working:

var google = require('googleapis');

var YOUTUBE_API_KEY = "--YOUR_API_KEY";

var youtube = google.youtube('v3');

var requests = youtube.search.list({part:'snippet', q: 'cats', maxResults: 10});

When I call this I get this message:

Error: Daily limit for Unauthenticated Used Exceeded.

Now, this is obviously because I'm not using my API key. However, I cannot find any resource out there that shows you how the API key is used for Node.

Anything I find tells me to do something like this:

var YOUTUBE_CLIENT_KEY = '';
var CLIENT_SECRET = '';
var REDIRECT_URL = '/';

var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
google.options({auth: oauth2Client});

Followed by my "youtube.search.list...".

The issue with this approach is I have NO idea where to get:

I cannot find any of these anywhere online. I have tried to just follow Javascript tutorials, since Node obviously uses Javascript, although it always requires the oAuth2... which requires the three things above.

Any helps/hints?

Upvotes: 1

Views: 8447

Answers (2)

devansvd
devansvd

Reputation: 1009

You should input api key in the auth parameter. This is the right way of doing it as per the https://github.com/google/google-api-nodejs-client/#google-apis-nodejs-client sample docs.

var google = require('googleapis');
var youtube = google.youtube({
   version: 'v3',
   auth: "your-api-key"
});


youtube.search.list({
    part: 'snippet',
    q: 'your search query'
  }, function (err, data) {
    if (err) {
      console.error('Error: ' + err);
    }
    if (data) {
      console.log(data)
    }
  });

Thanks

Upvotes: 5

Dolvik
Dolvik

Reputation: 100

It took me a while to find this point but I am a beginner on node.js and googleapis, so if someone else would like to expand this answer they can.

I believe to set simply using the Simple API key, just warning that you should only keep this key on the server side. Never give this key out, or it can be abused!

var googleapis = require('googleapis');
googleapis.client.setApiKey('YOUR API KEY');

Found this via https://developers.google.com/api-client-library/javascript/features/authentication under Simple access using the API key

Upvotes: 2

Related Questions