Gmail API Nodejs 和 PM2 身份验证 [英] Gmail API Nodejs and PM2 authentication

查看:102
本文介绍了Gmail API Nodejs 和 PM2 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Nodejs 中设置了使用 Gmail API 发送电子邮件的代码.它在本地机器上运行良好.它要求通过访问 URL 输入代码.类似的东西,

I have set up code to send emails using Gmail API in Nodejs. It works perfectly well in local machine. It asks to enter a code by visiting URL. Something like,

Authorize this app by visiting this URL: someURL
Enter the code from that page here: copy-paste code here

这是由 Google 按照 Node.js Gmail 快速入门自动完成的它会自动生成 token.json.在开发过程中一切正常.

This is automatically done by Google following Node.js Quickstart for Gmail and it automatically generates token.json. Everything works perfectly fine in development.

现在在生产中,我使用 PM2 和 Nginx 部署我的应用程序.所以,现在每个节点服务器都在后台运行,并在文件中生成日志.

Now in production, I deploy my app using PM2 and Nginx. So, now each node server is running in background and logs are generated in files.

我在日志文件中看到上述提示在此处输入该页面的代码:.但是当日志通过管道传输到文件时,我无法粘贴代码,因为我无法访问标准输入.

I see the above prompt in the log file Enter the code from that page here:. But as the log is piped to the file, I have no way of pasting the code since I don't have access to standard input.

我尝试从本地生成 token.json 并在似乎不起作用的服务器中使用它.

I have tried generating token.json from local and using it in the server which doesn't seem to work.

我该如何解决这个问题,有没有办法解决这个问题?

How do I fix this and is there any way around this?

注意:授权和发送邮件的代码工作正常.我不在这里发布它,因为我不希望问题变得混乱.

NOTE: The code to authorize and send mails works just fine. I'm not posting it here because I don't want the question to be cluttered.

推荐答案

如果您正在尝试制作不需要用户交互的服务器应用程序,而不是使用常规 OAuth,您可以尝试使用 服务帐户.

If you are trying to make a server application without need of user interaction instead of using regular OAuth you could try to use Service Account.

尽管考虑到服务帐户本身没有 Gmail API,因此您应该启用 域范围委派来冒充域的另一个用户.

Although take into account that service accounts don't have the Gmail API per se, so you should enable the Domain-Wide Delegation to impersonate another user of the domain.

如果您坚持要对真实用户进行身份验证,您可以在实施 PM2 之前检索 token.json 并在之后使用它.

If you insist in authenticating a real user, you can retrieve the token.json before implementing PM2 and using it afterwards.

你也可以看看这两个(12) github 帖子也开启了您尝试使用 stdin 来验证用户.

Also you can take a look at these two (1 and 2) github posts that also opens your initial thought of trying to use the stdin to authenticate the user.

这篇关于Gmail API Nodejs 和 PM2 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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