从谷歌api获取联系人

时间:2018-01-14 01:31:29

标签: javascript node.js rest google-contacts google-people

const express = require('express'),
  bodyParser = require('body-parser'),
  Sequelize = require('sequelize')
const app=express()
app.use(bodyParser.json())

var google = require('googleapis');
var contacts = google.people('v1');



const nconf = require('nconf');
const readline = require('readline');

const plus = google.plus('v1');
const path = require('path');
const OAuth2Client = google.auth.OAuth2;

nconf.argv().env().file(path.join(__dirname, '/oauth2.keys.json'));
const keys = nconf.get('web');

const CLIENT_ID = '1058912681476-uat19si2uli37vlehs2avqfue2l0b6ku.apps.googleusercontent.com';
const CLIENT_SECRET = 'PbY8AVICTQsywb4qiqCJ8gMB';
const REDIRECT_URL = 'urn:ietf:wg:oauth:2.0:oob';

const oauth2Client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

function getAccessToken (oauth2Client, callback) {
  // generate consent page url
  const url = oauth2Client.generateAuthUrl({
    access_type: 'offline', // will return a refresh token
    scope: 'https://www.googleapis.com/auth/plus.me' // can be a space-delimited string or an array of scopes
  });

  console.log('Visit the url: ', url);
  rl.question('Enter the code here:', code => {
    // request access token
    oauth2Client.getToken(code, (err, tokens) => {
      if (err) {
        return callback(err);
      }
      // set tokens to the client
      // TODO: tokens should be set by OAuth2 client.
      oauth2Client.credentials=tokens;
      callback();
    });
  });
}

// retrieve an access token
getAccessToken(oauth2Client, () => {
  // retrieve user profile
  plus.people.get({ userId: 'me', auth: oauth2Client }, (err, profile) => {
    if (err) {
      throw err;
    }
    console.log(profile.displayName, ':', profile.tagline);
  });
});







//here-not working
contacts.people.connections.list({
    auth: oauth2Client //authetication object generated in step-3
}, function (err, response) {
      // handle err and response
      if(err){
          throw err;
      }
      console.log(response.names);
});


app.listen(8080)

我正在尝试从google people api(https://developers.google.com/people/api/rest/v1/people.connections/list)获取用户的联系人,但是当我尝试调用contacts.people.connection.list()时,我收到错误:

缺少必需参数:resourceName     at Object.createAPIRequest(/home/ubuntu/workspace/contactmanager/backend/node_modules/googleapis/build/src/lib/apirequest.js:94:18);

2 个答案:

答案 0 :(得分:1)

错误说明了一切。 resourceName是必需参数。如果您查看people.connections.list,则必须提供people/me'值作为参数。其他值无效。

GET https://people.googleapis.com/v1/{resourceName=people/me}/connections

答案 1 :(得分:0)

@noogui已经回答了原因。这是用例的详细代码:

function listConnectionNames(auth) { const service = google.people({ version: "v1", auth }); service.people.connections.list({ resourceName: "people/me", pageSize: 10, personFields: "names,emailAddresses" }, (err, res) => { if (err) return console.error("The API returned an error: " + err); const connections = res.data.connections; if (connections) { console.log("Connections:"); connections.forEach(person => { if (person.names && person.names.length > 0) { console.log(person.names[0].displayName); } else { console.log("No display name found for connection."); } }); } else { console.log("No connections found."); } } ); }