0

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.

Kenmore
  • 1,525
  • 3
  • 16
  • 39

1 Answers1

0

I have partially solved my problem; at least, I have gotten the example in my question to work.

In the Developer Console I had to edit my service account and check a box to enable "Domain-wide delegation".

You go to your Service Accounts, click the little 3 dot menu icon to the right of your service account, click edit and in the popup dialog there is a checkbox that you need to check.

I had previously seen something about Domain-wide Delegation but in that example they were saying to go to your Google Apps account, which I don't have, so I thought it didn't pertain to me, but after tinkering around in the Developer Console I found that checkbox and the Gmail example above works, as well as other parts of the Gmail API including reading messages from my inbox.

However, I tried doing a request to the Calendar API and I am getting an error, but I think that calls for a different question.

Kenmore
  • 1,525
  • 3
  • 16
  • 39