我无法使用此Gmail API修改别名签名。我想修改具有user @ domain1,user @ domain2或user @ domain3的别名,这适用于某些用户,但不是所有用户。
我需要帮助,这是我的代码和错误:
我有一个要修改的用户列表,我制作了JWT并启动了功能。
编辑:当我尝试修改签名时,如果删除gmail.users.settings.sendAs.update,则会出现此错误,它将再次起作用。而且我可以修改primaryEmail的签名。
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const keys = require('./credentials.json');
const {JWT} = require('google-auth-library');
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/gmail.settings.basic',
'https://www.googleapis.com/auth/gmail.settings.sharing',
'https://www.googleapis.com/auth/gmail.modify',
'https://mail.google.com',
'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.readonly',
];
let list = [
user1, //working
user2, //working
user3, //not working
user4 //working
];
async function main() {
const client = new JWT(
{
email: keys.client_email,
key: keys.private_key,
subject: "admin@domain0",
scopes: SCOPES,
}
);
const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
listUsers(client);
}
main();
因此,我从管理google帐户中获取用户列表,并在列表中进行修改。我为每个用户生成新的JWT,并使用新的签名更改资源签名。
/**
* Lists the first 500 users in the domain.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listUsers(auth) {
const service = google.admin({version: 'directory_v1', auth});
service.users.list({
customer: 'my_customer',
maxResults: 500,
orderBy: 'email',
}, (err, res) => {
if (err) return console.error('The API returned an error:', err.message);
const users = res.data.users;
if (users.length) {
users.filter(utilisateur => list.findIndex(l => l === utilisateur.primaryEmail) !== -1).forEach((user) => {
//changer signature
if (user.primaryEmail && user.aliases) {
const client = new JWT(
{
email: keys.client_email,
key: keys.private_key,
subject: user.primaryEmail,
scopes: SCOPES,
}
);
client.subject = user.primaryEmail;
const gmail = google.gmail({version: 'v1', auth: client});
for(let jeton = 0; user.aliases[jeton] && jeton < user.aliases.length; jeton++) {
gmail.users.settings.delegates.list({userId:user.primaryEmail});
if (user.primaryEmail && user.aliases[jeton] && ((user.aliases[jeton].search("@domain1") !== -1) || (user.aliases[jeton].search("@domain2") !== -1) || (user.aliases[jeton].search("@domain3") !== -1))) {
gmail.users.settings.sendAs.update({
userId: user.primaryEmail,
sendAsEmail: user.aliases[jeton],
fields: 'signature',
resource: signature
});
}
}
}
})
} else {
console.log('No users found.');
}
});
console.log("Users has been modified");
}
function titleCase(str){
str = str.toLowerCase().split(' ');
let final = [ ];
for(let word of str){
final.push(word.charAt(0).toUpperCase()+ word.slice(1));
}
return final.join(' ')
}
(node:5284) UnhandledPromiseRejectionWarning: Error: Requested entity was not found.
at Gaxios.<anonymous> (C:Project_Directory\node_modules\gaxios\build\src\gaxios.js:73:27)
at Generator.next (<anonymous>)
at fulfilled (C:Project_Directory\node_modules\gaxios\build\src\gaxios.js:16:58)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:5284) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing insid
e of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate
the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.or
g/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5284) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections
that are not handled will terminate the Node.js process with a non-zero exit code.
我是NodeJS的新手,请保持温柔