I am utilizing Nodemailer with Gmail sign-in functionality. I have already configured my Service Account key and enabled the Gmail API in my Google Account. However, when I attempt to send an email using Nodemailer, I encounter the following error:
ERROR Error: unauthorized_client: Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.
Here is my code
const auth = await new GoogleAuth({
scopes: [
"https://mail.google.com/",
"https://gmail.googleapis.com",
"https://www.googleapis.com/auth/gmail.send",
],
authClient: GoogleAuthClient
});
const client = await auth.getClient();
const transporter = createTransport({
host: process.env.EMAIL_HOST as string,
name: process.env.EMAIL_HOST as string,
port: 465,
secure: true,
priority: "high",
auth: {
type: "OAuth2",
user: "[email protected]",
serviceClient: client.client_id,
privateKey: client.private_key,
accessUrl: client.token_uri,
accessToken: client.token_uri,
},
transactionLog: true,
});
const mailOptions = {
from: `"My Gmail Name" <${process.env.EMAIL_USERNAME}>`,
to: "[email protected]",
subject: options.subject,
html: options.html,
attachments: options?.attachments || [],
};
const resp = await transporter.sendMail(mailOptions);
System Requirements:
NodeJS: 18.12.0
NPM: 8.19.2
nodemailer: ^6.9.13
google-auth-library: ^9.10.0
I have reviewed the documentation and ensured that all prerequisites are met, but I am still unable to resolve this issue. Could you please assist me in identifying the cause of this error and provide guidance on how to rectify it?
Thank you in advance for your assistance.