简体   繁体   English

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

[英]Impersonate user with google calendar API oauth2 service account (node.js)

I am trying to use a server to server authentication through the google calendar API. 我试图通过谷歌日历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. 从我读过的所有内容中,'user @ example'应该指定模仿,但它不起作用。 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 = {};

thanks for any help!! 谢谢你的帮助!!

For anyone in the future who needs this. 对于将来需要这个的人。 It started working after: 它开始工作后:

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 1)启用域范围授权2)将用户添加到服务帐户作为所有者3)可能最重要**:继续谷歌管理员并给api访问服务帐户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM