|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 | // [START people_quickstart] |
| 17 | +/** |
| 18 | + * @typedef {Object} EmailAddress |
| 19 | + * @property {string} value |
| 20 | + */ |
| 21 | + |
| 22 | +/** |
| 23 | + * @typedef {Object} Name |
| 24 | + * @property {string} displayName |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * @typedef {Object} Person |
| 29 | + * @property {Name[]} names |
| 30 | + * @property {EmailAddress[]} [emailAddresses] |
| 31 | + */ |
| 32 | + |
| 33 | +/** |
| 34 | + * @typedef {Object} Connection |
| 35 | + * @property {Person[]} connections |
| 36 | + */ |
| 37 | + |
17 | 38 | /** |
18 | 39 | * Print the display name if available for 10 connections. |
19 | 40 | */ |
20 | 41 | function listConnectionNames() { |
21 | | - try { |
22 | | - /** |
23 | | - * List the 10 connections/contacts of user |
24 | | - * @see https://developers.google.com/people/api/rest/v1/people.connections/list |
25 | | - */ |
26 | | - const connections = People.People.Connections.list('people/me', { |
27 | | - pageSize: 10, |
28 | | - personFields: 'names,emailAddresses' |
29 | | - // use other query parameter here if needed. |
30 | | - }); |
31 | | - connections.connections.forEach((person) => { |
32 | | - // if contacts/connections is available, print the name of person. |
33 | | - if (person.names && person.names.length === 0) { |
34 | | - console.log('No display name found for connection.'); |
35 | | - return; |
36 | | - } |
37 | | - console.log(person.names[0].displayName); |
38 | | - }); |
39 | | - } catch (err) { |
40 | | - // TODO (developer) - Handle exception from People API |
41 | | - console.log('Failed with error %s', err.message); |
| 42 | + // Poll the People API to list the connections of the logged in user. |
| 43 | + // See: https://developers.google.com/people/api/rest/v1/people.connections/list |
| 44 | + if (!People || !People.People || !People.People.Connections) { |
| 45 | + // See: https://developers.google.com/apps-script/guides/services/advanced#enable_advanced_services |
| 46 | + throw new Error('People service not enabled.'); |
42 | 47 | } |
| 48 | + const connections = People.People.Connections.list('people/me', { |
| 49 | + pageSize: 10, |
| 50 | + personFields: 'names,emailAddresses', |
| 51 | + }); |
| 52 | + if (!connections.connections) { |
| 53 | + console.log('No connections found.'); |
| 54 | + return; |
| 55 | + } |
| 56 | + connections.connections.forEach((person) => { |
| 57 | + if (person.names && person.names.length > 0 && person.names[0].displayName) { |
| 58 | + console.log(person.names[0].displayName); |
| 59 | + } else { |
| 60 | + console.log('No display name found for connection.'); |
| 61 | + } |
| 62 | + }); |
43 | 63 | } |
44 | 64 | // [END people_quickstart] |
0 commit comments