javascript - 使用 Node.js 和 Google & Javascript 进行 OAuth 2 登录

标签 javascript node.js oauth

这是我之前提出的问题的后续内容; invalid_request from getToken in Javascript from Node.js

应用程序首先重定向到谷歌;

app.get('/GoogleLogin.html',function(req,res) {
var scopes = "";

// retrieve google scopes
scopes += config.google_login.scopes.baseFeeds + " "
scopes += config.google_login.scopes.calendar + " "
scopes += config.google_login.scopes.drive + " "
scopes += config.google_login.scopes.driveMetadata + " "
scopes += config.google_login.scopes.profile + " "
scopes += config.google_login.scopes.email + " "
scopes += config.google_login.scopes.tasks

var url = oauth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: scopes
});

res.writeHead(302, {location: url});
res.end();


});

它重定向到 google 以获取代码,然后当它返回时,它调用 oauth2Client.getToken 以便将该代码转换为 token 。

app.get('/AuthorizeGoogle.html',function(req,res) {
var queryData   = url.parse(req.url,true).query;
var code        = queryData.code;
var access_token = "";

oauth2Client.getToken(code,function(err,tokens) {
    if (err == null) {
        oauth2Client.setCredentials(tokens);        
        access_token = tokens.access_token;
        googleapis
            .discover('plus','v1')
            .withAuthClient(oauth2Client)
            .execute(function(err, client) {        
                if (err) {
                    console.log('An error occured');
                } 
                else {                      
                    getUserProfile(client, oauth2Client, 'me', function(err, profile) {
                        if (err) {
                            console.log('An error occured retrieving the profile');                                 
                        }
                        else {

                        }
                    });
                }

            });
    } 
    else {
        console.log("There seems to be a problem; " + err);
    }


    res.send("Authorized!");
});

就目前而言,这还算可以。然而,当我下次登录时,使用相同的谷歌帐户我会得到不同的代码和 token 。尽管原始登录和授权同时返回 token 和刷新 token - 后续尝试没有刷新 token 。

由于它们不同,因此无法判断用户是否已经登录以及我们是否已获得授权。

当我在 C# 中做同样的事情时 - 我可以使用 token 来搜索数据库并将其与用户链接。

那么,我如何使用 Google 登录并找到用户,而无需刷新 token - 因为稍后我将使用访问 token 来获取用户 Google 帐户的其他部分,例如文档、电子邮件、任务等。

提前致谢

最佳答案

好的,有点成功了。

基本上,我不使用 OAuth 2 进行登录和授权 - 我使用 OpenId 进行登录,使用 OAuth2 进行授权。

OpenId 功能由 Passport 和 Passport-google 提供。

var passport        = require('passport');
var GoogleStrategy  = require('passport-google').Strategy;

app.use(passport.initialize());
app.use(passport.session()); 

您需要初始化护照

passport.use(new GoogleStrategy({
    returnURL: 'http://localhost:81/AuthenticateGoogleLogin.html',
    realm: 'http://localhost:81/',
    clientID: config.google_login.client_id,
    clientSecret: config.google_login.client_secret,
},
function(identifier, profile, done) {
    return done(null,profile);
}));

然后您让 Google 登录用户

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

当 Google 回归时,使用之前创建的策略

app.get('/AuthenticateGoogleLogin.html', 
  passport.authenticate('google', { failureRedirect: '/Problem.html' }),
  function(req, res) {

});

处理登录和访问用户帐户的授权与我原来的问题完全相同。

关于javascript - 使用 Node.js 和 Google & Javascript 进行 OAuth 2 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21344690/

相关文章:

javascript - 索引数组的自然排序对象

JavaScript:如果键不存在则写入字典

javascript - AngularJS指令: Using ng-switch breaks transclusion

c++ - 使用 openssl 和 c++ 生成 OAuth 访问 token

javascript - 突然出现的 Javascript Google Oauth2 问题

ruby-on-rails - 如何使用 Facebook OAuth 和 Rails Devise gem 在 iOS native 应用程序中创建 session ?

javascript - 文本框的长度属性从不使用 JQuery 返回 0

javascript - 使用 Node js 和 socket.io 不会在客户端上触发事件

node.js - node-postgres 上的 postgres 复合类型

node.js -/lib64/libc.so.6 : version `GLIBC_2.14' not found. 为什么会出现此错误?