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