node.js - 如何在没有访问 token 的情况下获取 Google 联系人?

标签 node.js passport.js google-contacts-api

已更新

访问 token 的帮助下我仍然得到了答案:)

并且要提到它。

我正在使用 Google 联系人 API 获取用户 联系人。

我正在使用 Passport.js 登录 Google,并在 passport 的访问 token 的帮助下调用 API

https://www.google.com/m8/feeds/contacts/default/full?max-results=999999&alt=json&oauth_token=' + token

并获取所有联系人

但是我需要使用其他东西来代替访问 token ,例如 secret key 客户端 key

因为如果用户添加了新联系人,每次我都需要通过Google登录来同步联系人

我进行了 Google 但没有得到任何解决方案。

任何想法都会对我有帮助。

这是我获取联系人的代码

  var getGoogleContacts = function(token, userId) {
        var url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=999999&alt=json&oauth_token=' + token;
        request(url, function(error, response, body) {
            if (error) {
                console.log(error);
            } else {
                var contacts = JSON.parse(body);
                saveGoogleContacts(userId, contacts);
            }
        });

    };

    /*Get contacts and store in user_contact table*/
    var saveGoogleContacts = function(userId, contacts) {
        var gContacts = [];
        contacts.feed.entry.forEach(function(contact, index) {
            if (contacts.feed.entry[index].gd$email) {
                gContacts.push([
                    null, null, contacts.feed.entry[index].title.$t, "'" + contacts.feed.entry[index].gd$email[0].address + "'",
                    1, userId, 0
                ]);
            }
        });
        if (gContacts.length > 0) {
            user.insertContacts(gContacts, function(err, result) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('contacts saved: ' + result);
                }
            });
        }else{
            console.log('No records available');
        }
    }; 

最佳答案

我在这里得到了答案。

正如我提到的,我正在使用 passport.js 登录 Google

并且在登录过程中passport提供访问 token 刷新 token ,默认情况下刷新 token 将为null

如果你想获得刷新 token ,你需要在身份验证过程中传递参数 accessType:offline,就像这样

app.get('/auth/google', Passport.authenticate('google', { range : ['个人资料', '电子邮件','https://www.google.com/m8/feeds'],accessType: '离线',approvalPrompt: '强制' }));

之后,您将获得刷新 token 并将其存储在任何永久位置,因为它不会过期并且可以在您想要获取访问 token 时使用

要获取访问 token ,我正在使用 refresh-token模块。

获取token后只需调用API即可获取联系人。

像这样

var url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=999999&alt=json&oauth_token=' + token;
        request(url, function(error, response, body) {
            if (error) {
                cb(error);
            } else {
                var contacts = JSON.parse(body);
                cb(null, contacts);
            }
        });
    }

就是这样。

关于node.js - 如何在没有访问 token 的情况下获取 Google 联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939380/

相关文章:

javascript - 如何在后端 api 测试期间处理 csrf token

javascript - 是否可以在没有本地数据库的情况下使用 PassportJS?

google-contacts-api - 动态 Google API 重定向网址

Node.js/Socket.io/Passport.js 注销问题

node.js - 如何使用 Node.js、Express 和 Sequelize 进行身份验证?

javascript - Google 通讯录 API 错误

c++ - Visual Studio 多字节字符到单字节

javascript - Multer [NodeJS] 用于多个字段

node.js - 将Electron + Puppeteer打包为.exe

node.js - $limit 和 $skip 在 MongoDB 聚合管道中作为可选