Authenticating a service using the Google APIs in Nodejs

I am trying to use the YouTube V3 data API with Node, trying to provide an authenticated proxy server when doing search queries. Since the service will be running on the server, I created a service account.

This leads to the download of the key file and the following information:

I have a very general understanding of JWT, but the service section on the OAuth2 page suggested that you should use one of your libraries to do this, and not implement it yourself. However, they do not list the Node library in their supported libraries .

A little more digging and I came across a Google Nodejs client: https://github.com/google/google-api-nodejs-client , which claims to be supported by Google and has OAuth2 support out of the box.

The docs are pretty minimal, and authentication examples include typing a URL into a terminal and then visiting it in a browser to create a token. I have to dig around the source to find out if it supports JWT, and it looks like it has JWTClient .

/**
 * @constructor
 * JWT service account credentials.
 *
 * Retrieve access token using gapitoken.
 *
 * @param {string=} email service account email address.
 * @param {string=} keyFile path to private key file.
 * @param {array=} scopes list of requested scopes.
 * @param {string=} subject impersonated account email address.
 *
 */
function JWT(email, keyFile, key, scopes, subject) {
  JWT.super_.call(this);
  this.email = email;
  this.subject = subject;
  this.keyFile = keyFile;
  this.key = key;
  this.scopes = scopes;
  this.GAPI = GAPI;
}

This comment on the document over the constructor is apparently the only explanation here, and I still don't know how to use this function.

I guess:

  • email - email address of your developer account
  • keyFile is the path to the private key
  • keyis a private key? (If so, why include the path?)
  • scopes - API, .
  • subject

- ?

, , , , , 200 ( - ).

+4
1

, , .

JWT, , JWT.

https://github.com/google/google-api-nodejs-client/blob/master/examples/jwt.js

.

/**
 * @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 email address (optional).
 *
 */

// 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);
    });

  });

});

- , , . :

client.youtube.search.list({
 ... 
})
.withAuth(authClient)
.execute(...); 

- withAuth . , , , , , , authClient , .

err , POJO , , . [Object object] .

, , .

+11

Source: https://habr.com/ru/post/1537266/


All Articles