【发布时间】:2014-11-24 20:22:43
【问题描述】:
我一直在浏览 Google NodeJS API 文档,但没有看到为联系人 API 列出的文档。是我遗漏了什么还是模块中没有包含什么?
【问题讨论】:
标签: node.js google-contacts-api google-api-nodejs-client
我一直在浏览 Google NodeJS API 文档,但没有看到为联系人 API 列出的文档。是我遗漏了什么还是模块中没有包含什么?
【问题讨论】:
标签: node.js google-contacts-api google-api-nodejs-client
Google 的 NodeJS 官方 API 不使用 Contacts API。他们改用 People API。如果您需要访问“其他联系人”,则需要 Contacts API。
如果您已经将其用于其他目的,您仍然可以使用 official googleapis library 与联系人 API 连接,方法是在创建身份验证客户端后向联系人 API 发送请求。如果您不使用 googleapis 库,那可能是矫枉过正,最好使用其他答案建议的其他库。
鉴于您已经拥有用户的访问令牌(例如,如果您使用 Passport 生成它,代码如下:
const {google} = require("googleapis");
const authObj = new google.auth.OAuth2({
access_type: 'offline',
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
});
访问令牌过期前自动刷新
authObj.on('tokens', (tokens) => {
const access_token = tokens.access_token
if (tokens.refresh_token){
this.myTokens.refreshToken = tokens.refresh_token
// save refresh token in the database if it exists
}
this.myTokens.accessToken = tokens.access_token
// save new access token (tokens.access_token)
}
authObj.setCredentials({
access_token:this.myTokens.accessToken,
refresh_token:this.myTokens.refreshToken,
});
向联系人 API 发出请求:
authObj.request({
headers:{
"GData-Version":3.0
},
params:{
"alt":"json",
//"q":"OPTIONAL SEARCH QUERY",
//"startindex":0
"orderby":"lastmodified",
"sortorder":"descending",
},
url: "https://www.google.com/m8/feeds/contacts/default/full"
}).then( response => {
console.log(response); // extracted contacts
});
【讨论】:
根据 Google NodeJS API for Google Contacts API,以下链接可能对您有所帮助:
https://github.com/jimib/nodejs-google-contacts
【讨论】: