-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.html
63 lines (56 loc) · 2.21 KB
/
google.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://apis.google.com/js/client.js"></script>
<script>
function initAuth() {
gapi.auth.init(function() {
document.getElementById("authButton").removeAttribute("disabled");
});
}
var token;
function auth() {
var config = {
'client_id': '583962873515.apps.googleusercontent.com',
'scope': 'https://www.google.com/m8/feeds'
};
gapi.auth.authorize(config, function() {
// console.log('login complete: ' + JSON.stringify(gapi.auth.getToken()));
token = gapi.auth.getToken().access_token;
var request = new XMLHttpRequest();
request.onload = onContactsLoad;
var url = "https://www.google.com/m8/feeds/contacts/default/full" +
"?v=3.0&alt=json&access_token=" + encodeURIComponent(token);
request.open("GET", url, true);
request.send();
});
}
function joinEmails(aEmails) {
return (aEmails || []).map(function(e) { return e.address }).join(", ");
}
function joinPhones(aEmails) {
return (aEmails || []).map(function(e) { return e.$t }).join(", ");
}
function onContactsLoad() {
// console.log("xhr data = " + this.responseText);
var data = JSON.parse(this.responseText);
var entries = data.feed.entry || [];
document.getElementById("contactCount").textContent = "Fetched " + entries.length + " contacts from " + data.feed.id.$t + ".";
entries.forEach(function(e) {
var tr = document.createElement("tr");
var name = e.gd$name ? e.gd$name.gd$fullName.$t : "";
tr.innerHTML = "<td>" + name + "</td><td>" + joinEmails(e.gd$email) + "</td><td>" + joinPhones(e.gd$phoneNumber) + "</td>";
document.getElementById("googleContacts").appendChild(tr);
});
}
</script>
</head>
<body onload="initAuth();">
<h2>Google contacts</h2>
<button id="authButton" disabled="true" onclick="auth();">Load</button>
<p id="contactCount"></p>
<table id="googleContacts" border="1"><tr><th>Name</th><th>email</th><th>tel</th></tr>
</table>
</body>
</html>