使用Google Calendar API oauth2服务帐户(node.js)模拟用户

问题描述:

我正在尝试使用服务器通过Google日历API进行服务器身份验证.他们在文档中提到您可以冒充用户.我已为要模拟的帐户添加了所有者权限,并确保启用了域范围的委派.从我已阅读的所有内容中,"user @ example"应指定模仿者,但它不起作用.我具有创建活动等所有功能,但是除了Google随机生成的电子邮件以外,我无法通过其他电子邮件获得它.

I am trying to use a server to 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 everything I have read the 'user@example' should specify the impersonater 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 google randomly generated one.

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,
  'user@example.com'
);
jwtClient.authorize(function(err, token) {
  if(err) { 
    //console.log(err);
  }
  console.log('token',token);
  //listCalendars(jwtClient);
});

module.exports = {};

感谢您的帮助!

面向将来需要此功能的任何人.它在以下时间开始工作:

For anyone in the future who needs this. It started working after:

1)启用域范围的委派 2)将用户添加为所有者的服务帐户 3)可能是最重要的**:在google admin上授予了api访问服务帐户的权限

1) enabling domain-wide delegation 2) adding user to service account as owner 3) probably most important** : went on google admin and gave api access to the service account