Node.js 如何在没有访问令牌的情况下获取Google联系人?
已更新 我还是在Node.js 如何在没有访问令牌的情况下获取Google联系人?,node.js,passport.js,google-contacts-api,Node.js,Passport.js,Google Contacts Api,已更新 我还是在访问令牌的帮助下得到了答案 我想提一下 我正在使用googlecontactsapi获取user联系人 我正在使用passport.js登录谷歌,并在passport的访问令牌的帮助下调用API https://www.google.com/m8/feeds/contacts/default/full?max-results=999999&alt=json&oauth_-token='+token 以及获取所有联系人的信息 但是我需要使用其他东西,而不是访问令牌,比如密钥 或客户
访问令牌的帮助下得到了答案
我想提一下
我正在使用googlecontactsapi
获取user
联系人
我正在使用passport.js登录谷歌,并在passport的访问令牌的帮助下
调用API
https://www.google.com/m8/feeds/contacts/default/full?max-results=999999&alt=json&oauth_-token='+token
以及获取所有联系人的信息
但是我需要使用其他东西,而不是访问令牌
,比如密钥
或客户端密钥
因为每次我都需要使用谷歌登录来同步联系人
,如果用户添加了新联系人
我做了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
提供access token
和refresh token
默认情况下refresh token
将为null
如果您想要获得刷新令牌
,您需要在身份验证过程中传递参数
accessType:offline
,如下所示
app.get('/auth/google',passport.authenticate('google',{scope:['profile','email','https://www.google.com/m8/feeds'],accessType:'offline',approvalPrompt:'force'})代码>
之后,您将获得刷新令牌
并将其存储在任何永久位置,因为它不会过期
,并且可以在您想要获得访问令牌时使用
要获取访问令牌
,我正在使用模块
GERTINGtoken
后只需调用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);
}
});
}
就这样 请把你的问题解释清楚。你想在不使用访问令牌的情况下同步联系人(新添加的联系人)吗?是的,正如Google API所说,访问令牌将在一小时后过期,我正在维护我的本地会话一个月
,以便应用,因此我不需要在一个月前再次登录,所以,如果用户添加了新联系人,我需要一种不同的方法来获取新联系人。