Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88a92ca

Browse files
committed
Add people quickstart
1 parent e78853a commit 88a92ca

File tree

3 files changed

+329
-0
lines changed

3 files changed

+329
-0
lines changed

people/index.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
// [START people_quickstart]
18+
const fs = require('fs');
19+
const readline = require('readline');
20+
const {google} = require('googleapis');
21+
22+
// If modifying these scopes, delete token.json.
23+
const SCOPES = ['https://www.googleapis.com/auth/contacts.readonly'];
24+
const TOKEN_PATH = 'token.json';
25+
26+
// Load client secrets from a local file.
27+
fs.readFile('credentials.json', (err, content) => {
28+
if (err) return console.log('Error loading client secret file:', err);
29+
// Authorize a client with credentials, then call the Google Tasks API.
30+
authorize(JSON.parse(content), listConnectionNames);
31+
});
32+
33+
/**
34+
* Create an OAuth2 client with the given credentials, and then execute the
35+
* given callback function.
36+
* @param {Object} credentials The authorization client credentials.
37+
* @param {function} callback The callback to call with the authorized client.
38+
*/
39+
function authorize(credentials, callback) {
40+
const {client_secret, client_id, redirect_uris} = credentials.installed;
41+
const oAuth2Client = new google.auth.OAuth2(
42+
client_id, client_secret, redirect_uris[0]);
43+
44+
// Check if we have previously stored a token.
45+
fs.readFile(TOKEN_PATH, (err, token) => {
46+
if (err) return getNewToken(oAuth2Client, callback);
47+
oAuth2Client.setCredentials(JSON.parse(token));
48+
callback(oAuth2Client);
49+
});
50+
}
51+
52+
/**
53+
* Get and store new token after prompting for user authorization, and then
54+
* execute the given callback with the authorized OAuth2 client.
55+
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
56+
* @param {getEventsCallback} callback The callback for the authorized client.
57+
*/
58+
function getNewToken(oAuth2Client, callback) {
59+
const authUrl = oAuth2Client.generateAuthUrl({
60+
access_type: 'offline',
61+
scope: SCOPES,
62+
});
63+
console.log('Authorize this app by visiting this url:', authUrl);
64+
const rl = readline.createInterface({
65+
input: process.stdin,
66+
output: process.stdout,
67+
});
68+
rl.question('Enter the code from that page here: ', (code) => {
69+
rl.close();
70+
oAuth2Client.getToken(code, (err, token) => {
71+
if (err) return console.error('Error retrieving access token', err);
72+
oAuth2Client.setCredentials(token);
73+
// Store the token to disk for later program executions
74+
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
75+
if (err) console.error(err);
76+
console.log('Token stored to', TOKEN_PATH);
77+
});
78+
callback(oAuth2Client);
79+
});
80+
});
81+
}
82+
83+
/**
84+
* Print the display name if available for 10 connections.
85+
*
86+
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
87+
*/
88+
function listConnectionNames(auth) {
89+
const service = google.people({version: 'v1', auth});
90+
service.people.connections.list({
91+
resourceName: 'people/me',
92+
pageSize: 10,
93+
personFields: 'names,emailAddresses',
94+
}, (err, res) => {
95+
if (err) return console.error('The API returned an error: ' + err);
96+
const connections = res.data.connections;
97+
if (connections) {
98+
console.log('Connections:');
99+
connections.forEach((person) => {
100+
if (person.names && person.names.length > 0) {
101+
console.log(person.names[0].displayName);
102+
} else {
103+
console.log('No display name found for connection.');
104+
}
105+
});
106+
} else {
107+
console.log('No connections found.');
108+
}
109+
});
110+
}
111+
// [END people_quickstart]

people/package-lock.json

Lines changed: 201 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

people/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "google-people-nodejs-quickstart",
3+
"version": "1.0.0",
4+
"description": "A simple Node.js command-line application that makes requests to the Google People API.",
5+
"private": true,
6+
"dependencies": {
7+
"googleapis": "^27.0.0"
8+
},
9+
"devDependencies": {},
10+
"engines": {
11+
"node": ">=6.4.0"
12+
},
13+
"scripts": {
14+
"start": "node ."
15+
},
16+
"license": "Apache-2.0"
17+
}

0 commit comments

Comments
 (0)