I am trying to use a server for server authentication through the google calendar API. In the documentation they mention you can impersonate a user. I have added owner permissions to the account I want to impersonate and made sure the domain-wide delegation is enabled. From what I have read, the 'user@example' should specify the impersonator, but it does not work. I have all the functions of creating events etc working, but I can't get it to be from an email other than the randomly generated google one.
Here's my code:
var google = require('googleapis');
var calendar = google.calendar('v3');
var scopes = ['https://www.googleapis.com/auth/calendar'];
var key = require ('./xxx.json'); // private json
console.log("Calendar Service connected");
var jwtClient = new google.auth.JWT(
 key.client_email, 
 null, 
 key.private_key, 
 scopes,
 '[email protected]'
);
jwtClient.authorize(function(err, token) {
 if(err) { 
 //console.log(err);
 }
 console.log('token',token);
 //listCalendars(jwtClient);
});
module.exports = {};
 Andrei
 
 1,3712 gold badges18 silver badges41 bronze badges
 
 - 
 You might want to follow the possible solutions in this reported issue. You might have implemented your code differently and also make sure to Enable Domain-Wide Delegation for your service account. Hope this helps.Mr.Rebot– Mr.Rebot2017年09月01日 16:10:01 +00:00Commented Sep 1, 2017 at 16:10
- 
 See also stackoverflow.com/q/27956244/488666Maxime Pacary– Maxime Pacary2019年07月01日 11:22:13 +00:00Commented Jul 1, 2019 at 11:22
1 Answer 1
I got it to work after:
- Enabling domain-wide delegation
- Adding user to the service account as the owner
- Most importantly: Going onto google admin and giving api access to the service account
 Andrei
 
 1,3712 gold badges18 silver badges41 bronze badges
 
 
 answered Sep 1, 2017 at 16:48
 
 
 
 Dan Wolfgram 
 
 511 silver badge5 bronze badges
 
 
 Sign up to request clarification or add additional context in comments.
 
 
 
 2 Comments
BoredAndroidDeveloper
 I believe we've set this up but can't get it to work. @dan-wolfgram can you provide some clarity around those steps?
  jsmartt
 Can you please provide some more detail about which scopes you enabled with the domain-wide delegation? Also, please provide which APIs you enabled.
  lang-js