API Gmail in NodeJS: (node:14592) UnhandledPromiseRejectionWarning: Error: Delegation denied

API Gmail in NodeJS: (node:14592) UnhandledPromiseRejectionWarning: Error: Delegation denied

我在使用 GMAIL 时遇到了麻烦 API,这是我的错误:

(node:14592) UnhandledPromiseRejectionWarning: Error: Delegation denied for ADMIN_EMAIL

这是我的代码,我想先用域的 gsuits 管理员更改我的签名。当我尝试只检查并提示我的gmail信息帐户时,还可以,但是当我要修改签名时,出现错误。 我使用 ID 服务帐户在 Widedelegation 域中设置范围并下载 credentials.JSON

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',
];

async function main() {
    const client = new JWT(
        {
            email: keys.client_email,
            key: keys.private_key,
            subject: ADMIN_EMAIL,
            scopes: SCOPES,
        }
    );
    const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
    listUsers(client);
}

main();

/**
 * Lists all users in the domain and check if he's my email.
 *
 * @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: 50,
        orderBy: 'email',
    }, (err, res) => {
        if (err) return console.error('The API returned an error:', err.message);

        const users = res.data.users;
        if (users.length) {
            console.log('Users:');
            users.forEach((user) => {
                //credentials

                //changer signature
                if (user.primaryEmail && user.primaryEmail === MY_EMAIL) {
                    /*const client = new JWT(
                        {
                            email: keys.client_email,
                            key: keys.private_key,
                            subject: ADMIN_EMAIL,
                            scopes: SCOPES,
                        }
                    );*/
                    //client.subject = user.primaryEmail;
                    const gmail = google.gmail({version: 'v1', auth});
                    gmail.users.settings.delegates.list({userId:user.primaryEmail});
                    gmail.users.settings.sendAs.update({
                        userId: user.primaryEmail,
                        sendAsEmail: user.primaryEmail,
                        fields: 'signature',
                        resource: {
                            signature: SIGNATURE
                        }
                    });
                } else {
                    console.log('Error: Not found');
                }
            });
        } else {
            console.log('No users found.');
        }
    });
}

编辑:我发现了我的错误,就像 ziganotschka 说的,我需要创建一个新的 JWT 客户端。 这是新函数 listUsers:

function listUsers(auth) {
const service = google.admin({version: 'directory_v1', auth});
service.users.list({
    customer: 'my_customer',
    maxResults: 50,
    orderBy: 'email',
}, (err, res) => {
    if (err) return console.error('The API returned an error:', err.message);

    const users = res.data.users;
    if (users.length) {
        console.log('Users:');
        users.forEach((user) => {
            //changer signature
            if (user.primaryEmail && (user.primaryEmail === MY_SIGNATURE)) {
                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});
                gmail.users.settings.delegates.list({userId:user.primaryEmail});
                gmail.users.settings.sendAs.update({
                    userId: user.primaryEmail,
                    sendAsEmail: user.primaryEmail,
                    fields: 'signature',
                    resource: {
                        signature: SIGNATURE
                    }
                });
            } else {
                console.log('Error: Not found');
            }
        });
    } else {
        console.log('No users found.');
    }
});

}

要更改用户的签名,您必须模拟用户

  • 正如您已经做的那样,要创建/更新用户签名,您必须使用具有全域授权的服务帐户
  • 但是,如果服务帐户冒充管理员,您将收到错误 Delegation denied for ADMIN_EMAIL
  • 这是因为管理员无权更改除他自己以外的任何签名
  • 因此,您需要在循环 (users.forEach((user)) 中创建一个新的 JWT 客户端 - 将相应的用户电子邮件设为 subject - 对于每个用户