-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
96 lines (87 loc) · 2.82 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<title>RPC Batch: profile + email</title>
<style type="text/css">
.hide { display: none;}
.show { display: block;}
</style>
</head>
<body>
<div id="signin-button" class="show">
<div class="g-signin"
data-callback="signinCallback"
data-approvalprompt="force"
data-clientid="YOUR_CLIENT_ID"
data-scope="https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/auth/userinfo.email"
data-cookiepolicy="single_host_origin">
</div>
</div>
<div id="profile"></div>
<div id="email"></div>
<script>
function init() {
gapi.client.load('plus', 'v1');
gapi.signin.go();
}
/*
* Triggered when the accepts the the sign in, cancels, or closes the
* authorization dialog.
*/
function signinCallback(authResult) {
if (authResult) {
// Hide the sign in upon successful sign in.
if(authResult['access_token']) {
toggleElement('signin-button');
gapi.client.load('oauth2', 'v1', getProfileInfo);
}
else {
console.log(authResult['error']);
}
}
}
/*
* Register and sends requests to the batch
* https://code.google.com/p/google-api-javascript-client/wiki/RpcBatch
*/
function getProfileInfo(){
var rpcBatch = gapi.client.newRpcBatch();
var getProfile = gapi.client.plus.people.get({'userId': 'me'});
var getEmail = gapi.client.oauth2.userinfo.get();
rpcBatch.add(getProfile, { 'id': 'getProfile' });
rpcBatch.add(getEmail, { 'id': 'getEmail' });
rpcBatch.execute(getProfileCallback);
}
/*
* Call back for Batch, executed when all the requests in the batch
* have completed.
*/
function getProfileCallback(jsonResponse){
var profile = document.getElementById('profile');
var email = document.getElementById('email');
for (var id in jsonResponse) {
if (id == 'getProfile') {
var userProfile = jsonResponse[id].result;
profile.innerHTML = userProfile.displayName;
};
if (id == 'getEmail') {
jsonResponse[id].result;
email.innerHTML = jsonResponse[id].result.email;
}
}
}
function toggleElement(id) {
var el = document.getElementById(id);
if (el.getAttribute('class') == 'hide') {
el.setAttribute('class', 'show');
} else {
el.setAttribute('class', 'hide');
}
}
</script>
<script src="https://apis.google.com/js/client:plusone.js?onload=init">
{"parsetags": "explicit"}
</script>
</body>
</html>