已更新
在访问 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/