Roni Gadot Roni Gadot - 1 month ago 6
Node.js Question

Google prediction API v1.6 - How can I make server to server call?

I'm experimenting with the google prediction API V1.6. I've followed the getting started section, and created a model using the explorer API.

Now, my model is ready and the prediction results I get from the explorer are satisfying.

Now when I'm trying to move forward (using nodeJS), I'm lost ... I've looked at this library but couldn't figure out how to make a call to

prediction.trainedmodels.predict
without a client side consent screen.

Answer

Faced the same problem, here is what I found:

1 You'll need to create a service account.

  • Go to Google developers console
  • Click APIs & auth > Credentials
  • Add credentials for service account, download the generated service account's public/private key

Recommendation: Your application can complete these tasks either by using the Google APIs client library for your language, or by directly interacting with the OAuth 2.0 system using HTTP. However, the mechanics of server-to-server authentication interactions require applications to create and cryptographically sign JSON Web Tokens (JWTs), and it's easy to make serious errors that can have a severe impact on the security of your application.

For this reason, we strongly encourage you to use libraries, such as the Google APIs client libraries, that abstract the cryptography away from your application code.

  1. In your server code you should follow this steps
    • Create a JSON Web Token (JWT, pronounced "jot"), which includes a header, a claim set, and a signature.
    • Request an access token from the Google OAuth 2.0 Authorization Server.
    • Handle the JSON response that the Authorization Server returns.

use the google API node js lib to handle auth more easy.

var key = require('path/to/key.json');
var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, [scope1, scope2], null);

   jwtClient.authorize(function(err, tokens) {   if (err) {
       console.log(err);
       return;   
   }

     // Make an authorized request to list Drive files.
   drive.files.list({ auth: jwtClient }, function(err, resp) {
       // handle err and response   
   });
});