gpt4 book ai didi

node.js - Passport-google-oauth2 用于在 Node js 中获取 gmail.users.messages.list

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:06 25 4
gpt4 key购买 nike

首先,我对带有 Passport 的 Node js + oauth2 有点陌生,所以如果您有任何困惑,请评论。

我在下面尝试使用node.js从oauth2通过google进行身份验证,这是代码

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var google = require('googleapis');
var GOOGLE_CLIENT_ID = "---MY-CLIENT-ID";
var GOOGLE_CLIENT_SECRET = "---MY-CLIENT-SECRET";
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/', function(req, res){
res.render('index', { user: req.user });
});

app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user });
});

app.get('/login', function(req, res){
res.render('login', { user: req.user });
});

// user back to this application at /auth/google/return

app.get('/auth/google',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});

// GET /auth/google/return
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.

app.get('/auth/google/return',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});

app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});

app.listen(3000);

// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.

function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}

最重要的是工作正常,但现在我想在 Passport 的帮助下使用它来获取 gmail 的电子邮件列表。

   var gmail = google.gmail('v1');
var profileData = gmail.users.messages.list({ userId: 'me', auth: "**AUTHORISATION OBJECT WITH ACCESSTOKEN**" }, function(err, response) {
//console.log(response.messages);
});

我如何在 Passport 生成的访问 token 中使用它。

最佳答案

您需要一个 OAuth2 客户端:

var gmail = google.gmail('v1');
var OAuth2 = google.auth.OAuth2;

var oauth2Client = new OAuth2('<CLIENT_ID>', '<CLIENT_SECRET>', '<REDIRECT_URL>');
oauth2Client.setCredentials({ access_token: '<ACCESS_TOKEN_HERE>' });

app.get('/', function(req, res, next) {
gmail.users.messages.list({ userId: 'me', auth: oauth2Client },
function(err, response) {
res.send(response);
});
}

这对于使用您自己的凭据进行测试来说效果很好,但是对于更多用户,您必须将访问 token 更改为当前用户的 token 。

关于node.js - Passport-google-oauth2 用于在 Node js 中获取 gmail.users.messages.list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28984468/

25 4 0
文章推荐: c - 这是否违反了 `restrict` 的语义?
文章推荐: angularjs - 将消息绑定(bind)到 angularjs View
文章推荐: json - 使用gulp修改json文件
文章推荐: jquery - CSS 或 jQuery : text in two different colors in