3

我一直在浏览 Google NodeJS API 文档,但没有看到为 Contacts API 列出的文档。我错过了什么还是模块中没有包含?

4

2 回答 2

3

Google 的 NodeJS 官方 API 不使用 Contacts API。他们改用 People API。如果您需要访问“其他联系人”,则需要联系人 API。

如果您已经将其用于其他目的,您仍然可以使用官方 googleapis 库连接联系人 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
});
于 2019-07-18T20:08:12.223 回答
1

根据 Google NodeJS API for Google Contacts API,以下链接可能会帮助您:

https://github.com/jimib/nodejs-google-contacts

https://github.com/elentok/gcontacts

https://github.com/mattnull/node-googlecontacts

于 2014-11-25T17:51:06.260 回答