-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
146 lines (132 loc) · 3.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const { google } = require("googleapis");
const path = require("path");
const keyFile = path.join(__dirname, "./credential.json");
const scopes = ["https://www.googleapis.com/auth/contacts"];
const run = async () => {
const { people } = google.people({
version: "v1",
auth: await google.auth.getClient({
/* we need 2 things for the configuration of the client
1. Key File
2. Scopes
*/
keyFile,
scopes,
}),
});
// ? Create Contacts
people.createContact(
{
resource: {
names: {
givenName: "Muhammad",
familyName: "Zeeshan",
},
emailAddresses: {
value: "regalcoderz@gmail.com",
type: "home",
},
phoneNumbers: [
{
value: "+92 318 7649 354",
type: "home",
},
],
},
},
(err, res) => {
if (err) {
console.log("The API returned an error: " + err);
} else {
console.log(res.data);
}
}
);
// ? Get All Contacts
people.connections.list(
{
resourceName: "people/me",
personFields: "names",
},
(err, res) => {
if (err) return console.error("The API returned an error: " + err);
const gContacts = res.data.connections;
if (gContacts) {
console.log("Connections:");
gContacts.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.");
}
}
);
// ? Get Single Contact
people.get(
{
resourceName: "people/c4328366331976477502",
personFields: "names",
},
(err, res) => {
if (err) return console.error("The API returned an error: " + err);
const person = res.data;
if (person) {
console.log(person);
} else {
console.log("No person found.");
}
}
);
// ? Update Single Contact
people.updateContact(
{
resourceName: "people/c4328366331976477502",
personFields: "names,emailAddresses,phoneNumbers", // There should be no spaces between the commas, otherwise it returns an error.
updatePersonFields: "names,emailAddresses,phoneNumbers",
resource: {
// Person.metadata.sources.etag
etag: "%EgUBAi43PRoEAQIFByIMMVRobi9zZ29IY2M9",
names: {
givenName: "Muhammad",
familyName: "Zshaan",
},
emailAddresses: {
value: "muhammadzshaan@gmail.com",
type: "home",
},
phoneNumbers: [
{
value: "+92 318 7649 354",
type: "home",
},
],
},
},
(err, res) => {
if (err) {
console.log("The API returned an error: " + err);
} else {
console.log("Contact updated: " + res);
}
}
);
// ? Delete Single Contacts
people.deleteContact(
{
resourceName: "people/c3165744222806150724",
},
(err, res) => {
if (err) {
console.log("The API returned an error: " + err);
} else {
console.log("Contact deleted: " + res.data);
}
}
);
/* ================== End of Functions ================== */
};
run();