我设置了一个具有域范围委托的服务帐户,并传递了客户端电子邮件、私钥、作用域和用户电子邮件JWT方法,以模拟G-Suite用户。然后,我从Admin中获取特定的用户信息,并使用它创建电子邮件签名并将其推送到Gmail。如果我传递给JWT方法的用户电子邮件是一个超级管理员,那么它是很好的,但是如果我试图传递任何其他用户,我会得到一个错误响应,“未授权访问此资源/api”。对于如何让它在我的域中使用常规用户帐户,有什么想法吗?
,这是代码。(genSignature.js)
const { google } = require('googleapis');
const privatekey = require('../private-key.json');
const scopes = [
'https://www.googleapis.com/auth/gmail.settings.basic',
'https://www.googleapis.com/auth/gmail.settings.sharing',
'https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.directory.user.readonly'
];
const auth = async (user) => {
try {
const jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
scopes,
user // User who will be impersonated using the JWT client.
);
await jwtClient.authorize();
return jwtClient;
} catch (err) {
console.log(err.message);
};
};
function genSig(e) {
auth(e).then((jwtClient) => {
// Authenticate with the gmail API.
const gmail = google.gmail({
version: 'v1',
auth: jwtClient
});
// Authenticate with the admin API.
const dir = google.admin({
version: 'directory_v1',
auth: jwtClient
});
// Get users contact and job data from the directory. This data will be used as variables in their email signature.
dir.users.get({ userKey: e }, (err, response) => {
if (err) {
console.log(err.message);
} else {
let phones = response.data.phones;
let workPhone = '';
if (phones) {
for (i = 0; i < phones.length; i++) {
if (phones[i].type == 'work') {
workPhone = phones[i].value;
};
};
};
function getUserData() {
let userData = {
name: response.data.name.fullName,
email: response.data.primaryEmail,
phone: workPhone,
avatar: response.data.thumbnailPhotoUrl,
department: response.data.organizations[0].department,
title: response.data.organizations[0].title
};
return userData;
};
let requestBody = {
signature: 'Test'
};
// Update the users email signature for their primary email.
gmail.users.settings.sendAs.update({ userId: e, sendAsEmail: e, requestBody }, (err, response) => {
if (err) {
console.log(err.message);
} else {
console.log(response.data);
};
});
};
});
});
}
module.exports = genSig;(signatures.js)
const express = require('express');
const router = express.Router();
const genSig = require('../../functions/genSignature');
// Get webhooks from Google.
router.post('/', (req, res) => {
let email = req.body.email;
let emailStr = email.toString();
console.log(emailStr);
genSig(emailStr);
res.status(200).json({
"msg": "data recieved..."
});
});
module.exports = router;(index.js)
const express = require('express');
const app = express();
app.use(express.json());
app.use('/email-signature', require('./routes/api/signatures'));
const PORT = process.env.PORT || 6000;
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));这里有一些截图。
发布于 2020-11-03 09:42:01
您需要模拟管理员:
只有具有用户管理权限的帐户(如超级管理或用户管理管理)才能访问用户:获取。您必须考虑到这是管理SDK的一部分,它将由管理帐户使用。
如果尝试通过Try this API 关于参考文档调用它,也可以检查这是不可能的(您将得到相同的消息:Not Authorized to access this resource/api)。
使用具有域范围权限的服务帐户并不重要:当服务帐户模拟另一个用户时,它只能访问该用户可以访问的资源。
解决方案:
在这种情况下,如果要从Admin检索用户数据,则模拟帐户应该具有用户管理权限。
但是,由于调用Gmail方法不需要这些特权,所以在调用Users: get时可以模拟管理帐户,在调用users.settings.sendAs.update时可以模拟常规帐户。
参考资料:
https://stackoverflow.com/questions/64657836
复制相似问题