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

自闭症网瘾萝莉.ら 提交于 2021-01-27 06:36:48

问题


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 = {};

thanks for any help!!


回答1:


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



来源:https://stackoverflow.com/questions/45995671/impersonate-user-with-google-calendar-api-oauth2-service-account-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!