I am trying to connect to a google calandar and edit it via the rest api from a nodejs app. I am not authenticating users here, it is a single calendar owned by the business.
I have set up a service account and have the private credentials to retreive the access token.
According to the documantation i need to use jwt to retrieve the access token.
This is my code with my private dcredentials removed.
The request is successful but I continuously get an "error" : "invalid_grant" response
var moment = require('moment');
var https = require('https');
var jwt = require('jwt-simple');
var querystring = require('querystring');
var payload = {
"iss":"", // I put my client_id email in here
"scope":"https://www.googleapis.com/auth/calendar",
"aud":"https://accounts.google.com/o/oauth2/token",
"exp": moment().add(1, 'hour').valueOf(),
"iat": moment().valueOf()
}
var secret = ''; // I put my secret in here - e.g. '-----BEGIN PRIVATE KEY xxxxx
var token = jwt.encode(payload, secret, 'RS256');
var data = querystring.stringify({
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
assertion: token
});
var options = {
host: 'accounts.google.com',
path: '/o/oauth2/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("body: " + chunk);
});
});
req.write(data);
req.end();
Any help appreciated.