1

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:

  • CLIENT_ID
  • CLIENT_SECRET
  • REDIRECT_URL

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?

Jake Alsemgeest
  • 692
  • 2
  • 13
  • 25
  • Did you check the "Using API keys" session in the readme ? You have to pass the API key with `key` or `auth` param when making a request. – limekin Jul 06 '15 at 03:56
  • Wow, well I feel a little stupid. This does make it so I can query, although it just gives me a huge JSON object. I don't know if it actually contains possible youtube videos though, or it doesn't appear to. – Jake Alsemgeest Jul 06 '15 at 04:04
  • You get the CLIENT_ID and SECRET at Google Developers Console : https://console.developers.google.com/project , once you create a project in there. – limekin Jul 06 '15 at 04:04
  • @limekin all I can see is a project ID. When I go to APIs & auth > Credentials there is no SECRET at all either. Also, I found that it provides a URL that contains the JSON for the information on the query. Although is it possible I'm missing that information somewhere within the initial JSON object? Or do I just need to use that URL in order to get the information? – Jake Alsemgeest Jul 06 '15 at 04:10
  • Ah, you can check out the reference of YouTube Data API for what you can send and receive. Since you are trying out list check list in here : https://developers.google.com/youtube/v3/docs/ – limekin Jul 06 '15 at 04:12
  • Make sure you check the `list` under the 'Search` ! – limekin Jul 06 '15 at 04:14
  • About client secret this should help : http://stackoverflow.com/questions/11295661/google-apis-console-missing-client-secret – limekin Jul 06 '15 at 04:21
  • I'm using the new Google Cloud console. I can't see anything that the post is talking about unfortunately. I also can't seem to find a way to revert to the older version of the console. – Jake Alsemgeest Jul 06 '15 at 17:08
  • If I go to: https://developers.google.com/api-client-library/javascript/features/authentication I click on Google Developers Console and I can get to the old one where I can find the SECRET KEY. – Jake Alsemgeest Jul 06 '15 at 17:41

2 Answers2

5

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

devansvd
  • 889
  • 1
  • 12
  • 23
  • 1
    if you get and error, try this : `const {google} = require('googleapis');` – Anatole Lucet Jan 08 '19 at 18:19
  • Now this is updated to var google = require('googleapis'); var youtube = new google.youtube_v3.Youtube({ version: 'v3', auth: KEY }); youtube.search.list({ part: 'snippet', q: 'channelId=UCJrDMFOdv1I2k8n9oK_V21w' }, function (err, data) { if (err) { console.error('Error: ' + err); } if (data) { console.log(data) } }); – maddy Jun 03 '20 at 07:12
2

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

Dolvik
  • 100
  • 13