Gmail API NodeJS - UnhandledPromiseRejectionWarning: Error: Requested entity was not found

524 Views Asked by At

I'm in trouble with this Gmail API for modifying signature of aliases. I want to modify aliases who have user@domain1, user@domain2 or user@domain3That's work with some users, but not all the users.

I'm at my limit I need help, here is my code and the error:

I have a list of users me wanting to modify, I made the JWT and start my function.

EDIT: This error appear when I try to modify signature, if I delete gmail.users.settings.sendAs.update, this works again. And I can modify signature of 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();

So, I take users list of my admin google account and modify the users in the list.I generate new JWT for each user and change the resource signature with the new signature.

/**
 * 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.

I'm new with NodeJS, so please be gentle

0

There are 0 best solutions below