I have a form on a website that needs to check my calendar to see what times are available, and then later when it's submitted it's going to send an email.
I created a service account in the API console but I have no idea how to grant it access to my account.
Here's what I have so far, trying to list labels of my Gmail account as a test.
var google = require( 'googleapis' );
var gmail = google.gmail('v1');
var key = require('../client_secret.json');
var authClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://mail.google.com/'],
'****@gmail.com');
authClient.authorize(function (err, tokens) {
if (err) {
return console.log(err);
}
gmail.users.labels.list({
auth: authClient,
userId: '****@gmail.com'
}, function (err, resp) {
if (err) {
return console.log(err);
}
console.log(resp);
});
});
And when I run the script I get:
[Error: unauthorized_client]
Edit:
I've also tried another auth flow using oauth, where it takes you to the typical permission screen asking you to Allow access for "project name" to "whatever scopes you put". Then I can use the access token to make requests and everything is good... but this doesn't feel right because I don't ever want any other user to authorize. I could use a route to authorize the "app" 1 time and then disable that page so it can't be accessed again... It feels kind of hack-y.