Nodemailer/Gmail - 究竟什么是刷新令牌,我如何获得一个? [英] Nodemailer/Gmail - What exactly is a refresh token and how do I get one?

查看:18
本文介绍了Nodemailer/Gmail - 究竟什么是刷新令牌,我如何获得一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 nodemailer 在节点应用程序中创建一个简单的联系表单.我希望所有的味精都从我为此目的创建的 gmail 帐户发送到我的个人邮件.

在客户端,我所做的只是获取客户的姓名/邮件/消息并将其发送到服务器.它在本地运行良好,但在部署时无法运行(顺便说一句,在 heroku 上).

快速搜索后,我似乎必须从 Google Developers Console 生成 ClientIdClientSecret - 我这样做了 - 但是在生成刷新令牌"我完全迷路了.

 var smtpTransport = nodemailer.createTransport("SMTP",{服务:Gmail",授权:{XOAuth2:{用户:myaccount@gmail.com",客户编号:"",客户秘密:",刷新令牌:"}}});

我很困惑:究竟什么是刷新令牌,如何获得?

解决方案

本答案原作者注:

<块引用>

所以,我终于设法弄清楚了.我很惊讶我找不到更多关于此的资源,所以对于那些需要将 GmailNodemailer

一起使用的人

我在这里找到了答案: 获取刷新令牌

  1. 转到 ​​Google Oauth2.0 Playground.
  2. 点击右上角的齿轮按钮.设置您从 Google Developers ConsoleClient ID 和 Client Secreta>,然后选择 Access token location 作为Authorization header w/Bearer prefix.关闭此配置覆盖.

  1. 设置范围.使用 https://mail.google.com/ 因为它是 nodemailer 需要的.然后点击授权 API 按钮.

  1. OAuth2.0 授权后,用授权码交换令牌,瞧!您的刷新令牌可以使用了

I'm trying to do a simple contact form in a node app, using nodemailer. I want all the msg to be sent from a gmail account I made for this purpose, to my personnal mail.

on the client side, all I do is to get the name/mail/message of the customer and send it to the server. It works fine locally but fails to work when deployed (on heroku btw).

After a quick search, it seems I have to generate a ClientId and ClientSecret from Google Developers Console - which I did - but when it comes to generating a "refresh token" iI'm completely lost.

    var smtpTransport = nodemailer.createTransport("SMTP",{
        service:"Gmail",
        auth:{
            XOAuth2: {
                user:"myaccount@gmail.com",
                clientId:"",
                clientSecret:"",
                refreshToken:""
            }
        }
    });

I am confused : What exactly is a refresh token and how do I get one ?

解决方案

Notes by this answer original's author:

So, I finally managed to figure it out. I'm surprised I couldn't find more ressources about that so for those who need to use Gmail with Nodemailer

I found the answer here: http://masashi-k.blogspot.fr/2013/06/sending-mail-with-gmail-using-xoauth2.html

Try creating a new User if you already had one and things ain't working fine. It was the case for me.

I hope this will be useful to someone,

Cheers


Question 1: What exactly is a refresh token?

From documentation found here:

A refresh token provides your app continuous access to Google APIs while the user is not logged into your application.

(...)

Considerations:

  • Be sure to store the refresh token safely and permanently, because you can only obtain a refresh token the first time that you perform the code exchange flow.

  • There are limits on the number of refresh token that are issued—one limit per client/user combination, and another per user across all clients. If your application requests too many refresh tokens, it may run into these limits, in which case older refresh tokens stop working.

See also Offline Access and Using a refresh token.


Question 2: How do I get one?

Step 1: Obtain OAuth 2.0 credentials at Google Developers Console

As stated here, you should:

  1. Go to the Google Developers Console.
  2. Select a project, or create a new one.
  3. In the sidebar on the left, expand APIs & auth. Next, click APIs. Select the Enabled APIs link in the API section to see a list of all your enabled APIs. Make sure that the "Gmail API" is on the list of enabled APIs. If you have not enabled it, select the Gmail API from the list of APIs (under Google Apps APIs), then select the Enable API button for the API.
  4. In the sidebar on the left, select Credentials.
  5. If you haven't done so already, create your project's OAuth 2.0 credentials by clicking Create new Client ID, and providing the information needed to create the credentials.

  1. Look for the Client ID and Client secret in the table associated with each of your credentials.


PAY SPECIAL ATTENTION TO specifying https://developers.google.com/oauthplayground as a Redirect URI when you create a new User in the console. Otherwise, you will have an error.


Step 2: Obtain the refresh token at Google OAuth2.0 Playground

  1. Go to the Google Oauth2.0 Playground.
  2. Click the Gear Button on the right-top. Set your Client ID and Client Secret obtained from the Google Developers Console, and select Access token location as Authorization header w/ Bearer prefix. Close this configuration overlay.

  1. Set up the scopes. Use https://mail.google.com/ as it's the one need by nodemailer. Then click the Authorize APIs button.

  1. After OAuth2.0 authorization, exchange authorization code for tokens and voilá! your refresh token is ready-to-use

这篇关于Nodemailer/Gmail - 究竟什么是刷新令牌,我如何获得一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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