I've managed to piece together a working solution, so I'll leave it here for anyone else who has a similar problem in the future.
There is a JWT example in the repo, which shows in more detail how you use the constructor for the JWT object.
https://github.com/google/google-api-nodejs-client/blob/master/examples/jwt.js
Here's a slightly amended version of the doc comment.
/**
* @constructor
* JWT service account credentials.
*
* Retrieve access token using gapitoken.
*
* @param {string=} email service account email address from developer console.
* @param {string=} keyFile absolute path to private key file.
* @param {string=} key the contents of the key if you are loading it yourself (optional)
* @param {array=} scopes list of requested scopes.
* @param {string=} subject impersonated account's email address (optional).
*
*/
And the finished code looks like this
// Create the JWT client
var authClient = new googleapis.auth.JWT(email, keypath, key,
['https://www.googleapis.com/auth/youtube']);
// Authorize it to produce an access token
authClient.authorize(function(err, tokens) {
if(err) throw err;
// Use discovery to get the youtube api
googleapis.discover('youtube', 'v3')
.execute(function(err, client) {
// Create a search
var search = client.youtube.search.list({
q: '<query string>',
part: 'snippet',
maxResults: 50
});
// Authenticate with current auth client
search.authClient = authClient;
// Hey presto!
search.execute(function(err, results) {
if(err) throw err;
console.log(results.items);
});
});
});
This code example is a mess, but it's enough to give you the idea. Based on the example you should be able to do:
client.youtube.search.list({
...
})
.withAuth(authClient)
.execute(...);
But for some reason the withAuth
method isn't there. It took a bit of digging to work out what it did and as far as I can see, it works fine if you set the authClient
property by hand, as shown above.
As a side note, the err
arguments are not errors, but POJOs and throwing them as above doesn't actually work. Unless you're happy to see [Object object]
as your debug info.
Hopefully that library will get some proper documentation attention soon and problems like this will be ironed out.