无法在Node.js中将Gmail联系人与passport-google-oauth同步
我是一个新的passport.js。 我正在使用它来validation和获取Gmail contacts
,以获取我需要通过scope
值这个https://www.google.com/m8/feeds
的联系人。但我没有得到联系人列表,除了configuration文件的详细信息。
这是我的代码的东西:
//register with google app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email','https://www.google.com/m8/feeds'] })); // the callback after google has authenticated the user app.get('/auth/google/callback', passport.authenticate('google', { successRedirect : '/user/dashboard', failureRedirect : '/register.html' }));
和我的passport.js
代码:
passport.use(new GoogleStrategy1({ consumerKey: config.auth.googleAuth.clientID, consumerSecret: config.auth.googleAuth.clientSecret, callbackURL: config.auth.googleAuth.callbackURL }, function(token, tokenSecret, profile, done) { console.log(profile); return done(err, user); } ));
当我打印profile
我只获取用户详细信息而不是contact list
我不知道,我做了什么。 任何帮助将不胜感激。
谢谢。
以下是用于获取Gmail contacts
1-为了与任何Google API进行通信,我们需要在Google控制台上创build一个帐户
2-创build项目后,我们要与Google API进行通信,创build项目后,Google提供用于与Google通信的secret key
和client key
。 每当我们的应用程序尝试与任何Google API进行通信时,都需要这些密钥。
3-为了获得Gmail通讯录,Google提供了https://www.google.com/m8/feeds/contacts/default/full?hl=zh-CN&oauth_token=xxxxxx
4-我们只需要调用这个API来获取联系人,这个API在通信之前需要一些凭证。
5-用户必须使用Googlelogin并获得API用来获取用户联系人的令牌。
6-通常我们更喜欢护照Google的策略来loginGoogle。 而我们的一半是通过Passport.js
来完成的,比如使用Google和令牌进行身份validation。
7-当用户使用Googlelogin时,Passport.js扮演成功login的中间件的angular色,在此期间,护照提供当前用户的令牌。 而那段时间,我们正在调用联系人API https://www.google.com/m8/feeds/contacts/default/full?hl=zh-CN&auth_token=xxxxxx
而且我们很容易就能得到令牌,Google创build的令牌会在一小时后过期,但是我们不应该担心,因为护照在内部提供了由Google提供的新令牌。
希望它能为你工作。
更新
让我们玩REST API
通过使用REST call
获取所有联系人
使用请求模块来请求HTTP
调用
request.get({ url: 'https://www.google.com/m8/feeds/contacts/default/full', headers: { 'Authorization': 'Bearer <Your access token>', 'Content-Type': 'application/json' }, qs: qs,//Optional to get limit, max results etc method: 'GET' }, function (err, response, body) { });