Nodemailer Oauth2错误:authorized_client [英] Nodemailer Oauth2 error : unauthorized_client

查看:139
本文介绍了Nodemailer Oauth2错误:authorized_client的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在nodejs应用上使用Oauth和nodemailer发送邮件,我在没有Oauth的情况下进行了邮件发送,但是我的密码写在了代码中,所以我将自己转为Oauth.

I'am trying to send mail using Oauth and nodemailer on a nodejs app, I did it without Oauth but my password was wrote in the code so I turn myself to Oauth.

我只想连接自己以自动方式发送邮件. 我已经在Google Cloud Platform上建立了一个项目和一个服务帐户.我添加了gmail api并编写了一些代码:

I only want to connect myself to send mail in an automatic way. I have settup a project and a service account on google cloud platform. I added the gmail api and wrote some code :

 var smtpTransport = nodemailer.createTransport({
   host:'smtp.gmail.com',
   port:465,
   secure:true,
   auth:{
     type: 'OAuth2',
     user: 'thomas.legrand.test@gmail.com',
     serviceClient:config.client_id,
     privateKey:config.private_key
   }
 });
 var mail = {
   from: "thomas.legrand.test@gmail.com",
   to: "thomas.legrand26@gmail.com",
   subject:"Un sujet abstrait",
   html:"<h1> Ceci est un mail de test </h1><h2> et ceci un sous titre </h2> "
 };

 smtpTransport.on('token', token => {
    console.log('A new access token was generated');
    console.log('User: %s', token.user);
    console.log('Access Token: %s', token.accessToken);
    console.log('Expires: %s', new Date(token.expires));
});

smtpTransport.sendMail(mail, function(error, response) {
        if(error) {
          console.log("Erreur lors de l'envoie du mail ");
          console.log(error);
        }else {
          console.log("succes")
        }
        smtpTransport.close();
      });

但是我收到一个我无法解决的错误(未经授权的客户端).

But I get an error (unauthorized_client) which I can't solve.

希望您能帮到我或至少给我提示!

I hope you can help me or give me hints at least !

推荐答案

请确认您遵循以下步骤,以确保以正确的方式设置客户端:

Verify you follow these steps to be sure you set in the right way your client:

然后别忘了启用Gmail API,如此处所述:

And then don't forget to enable your Gmail API as it is said here:

此外,我建议您检查 Gmail API快速入门.

Also, I would recommend you to check the Gmail API Quickstart.

这篇关于Nodemailer Oauth2错误:authorized_client的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆