我有一个NodeJs Google云项目,该项目运行正常,并且我正在使用Google+登录获取用户个人资料,但我从Google收到一条消息,说Google+登录已被完全弃用,并且还将在2019年3月7日。
我如何不使用方法plus.people.get
这是我的身份验证功能:
module.exports = function(config) {
var googleapis = require('googleapis');
var request = require('request');
var client = new googleapis.auth.OAuth2(
config.oauth2.clientID,
config.oauth2.clientSecret,
config.oauth2.redirectUrl
);
function getAuthenticationUrl() {
return client.generateAuthUrl({
access_type: 'offline',
prompt: 'consent',
scope: [
'email',
'profile',
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/gmail.send'
]
});
}
function refreshAccessToken(tokens, callback){
client.setCredentials(tokens)
client.refreshAccessToken(function(err, tokens){
console.log(err)
client.setCredentials(tokens);
callback(err, tokens)
});
}
function getUserStandard(authorizationCode, callback){
getUser(authorizationCode, client, callback)
}
function getUser(authorizationCode, client, callback) {
// With the code returned from OAuth flow, get an access token
client.getToken(authorizationCode, function(err, tokens) {
if (err) return callback(err);
// Configure this Google API client to use the access token
console.log("------------ getToken ---------------")
console.log(tokens)
console.log(tokens.refresh_token)
client.setCredentials({
access_token: tokens.access_token,
refresh_token: tokens.refresh_token,
id_token: tokens.id_token,
token_type: tokens.token_type,
expiry_date: tokens.expiry_date
});
//I bealive that this code here should be changed according to the message recieved from Google.
// Call the Google+ API to get the profile of the user who authenticated
googleapis.plus('v1').people.get({ userId: 'me', auth: client },function(err, profile) {
if (err) return callback(err);
console.log(profile)
var user = {
id: profile.id,
name: profile.displayName,
imageUrl: profile.image.url,
email: profile.emails[0].value,
tokens: tokens
};
callback(null, user);
});
});
}
return {
getAuthenticationUrl: getAuthenticationUrl,
getUserStandard: getUserStandard,
refreshAccessToken: refreshAccessToken
};
};
更新:我根据Google文档Authenticating Users with Node.js进行了一些更改。
身份验证工作正常,但是在调用登录回调时我无法将用户信息存储到会话中,但是我将用户存储在会话中,但是当用户重定向到原始URL时,我丢失了信息存储。
app.js:
app.get('/', function (req, res, next) {
req.session.url = "/";
console.log("********************* Default ***********************")
console.log(req.user)
console.log(req.session.url)
console.log(req.session.user)
if (!req.session.user){
console.log("------------ No session ------------")
authRequired(req, res, next);
}else{
var languageInfo = util.getLanguageJSON(req);
var languageJSON = languageInfo[0];
var LanguageName = languageInfo[1];
res.render('index.ejs', {user:req.session.user, i18n:languageJSON, languageName: LanguageName});
}
});
function authRequired (req, res, next) {
return res.redirect('/auth/login');
}
app.get('/auth/login',
(req, res, next) => {
console.log(req.user)
if (req.query.return) {
req.session.oauth2return = req.query.return;
}
next();
},
passport.authenticate('google', {
scope: [
'profile',
'email',
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/gmail.send'
]
})
);
app.get('/auth/google/callback',
passport.authenticate('google'),
function(req, res, next) {
console.log("********************* oauth2callback ***********************")
req.session.user = req.user;
req.session.user.email = req.session.user.email.toLowerCase();
console.log(req.session.user)
if(req.session.url == undefined){
req.session.url = "/";
}
return res.redirect(req.session.url);
});
auth.js:
passport.use(new GoogleStrategy({
clientID: config.oauth2.OAUTH2_CLIENT_ID,
clientSecret: config.oauth2.OAUTH2_CLIENT_SECRET,
callbackURL: config.oauth2.OAUTH2_CALLBACK,
accessType: 'offline',
prompt: 'consent'
}, (accessToken, refreshToken, params, profile, callback) => {
// Extract the minimal profile information we need from the profile object
// provided by Google
let imageUrl = '';
if (profile.photos && profile.photos.length) {
imageUrl = profile.photos[0].value;
}
var profile = {
id: profile.id,
name: profile.displayName,
imageUrl: imageUrl,
email: profile.emails[0].value,
tokens: params
};
callback(null, profile);
}));
passport.serializeUser((user, cb) => {
cb(null, user);
});
passport.deserializeUser((obj, cb) => {
cb(null, obj);
});
我的代码有问题吗?