forked from shantanu21/signup_with_google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
206 lines (190 loc) · 6.54 KB
/
index.php
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
session_start();
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Goole OAuth Gmail contacts import</title>
<meta content=' Goole OAuth Gmail contacts import.' name='description'/>
<meta content='Google, Oauth, php, Gmail, Contacts,import' name='keywords'/>
<script type="text/javascript">
(function() {
var po = document.createElement('script');
po.type = 'text/javascript'; po.async = true;
po.src = 'https://plus.google.com/js/client:plusone.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
})();
function onSignOut()
{
gapi.auth.signOut();
}
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" ></script>
<meta content=' Goole OAuth Gmail contacts import.' name='description'/>
<meta content='Google, Oauth, php, Gmail, Contacts,import' name='keywords'/>
</head>
<body style="background-color:#CCCCCC; font-family:'Century Gothic';">
<div width="800px" align="center">
<div id="gConnect" style="border:1px solid; margin:200px 500px 0px 450px; padding-bottom:15px;">
<p style="font-family:'Century Gothic'; font-size:25px">Login with Google+: </p>
<button class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-clientId="34760705102-aolcts0u7j60maegrc1uc5rqu7tlp149.apps.googleusercontent.com"
data-accesstype="offline"
data-callback="onSignInCallback"
data-theme="dark"
data-cookiepolicy="single_host_origin">
</button>
</div>
</div>
<div id="authOps" width="100px" style="display:none; margin-left:450px;">
<h2>To Import Contacts click <?php include('GmailConnect.php'); ?></h2>
<p>User is now signed in to the app using Google+</p>
<button id="disconnect" onclick="onSignOut()" >Sign Out</button>
<h2>Profile Information</h2>
<div width="200px" style="border:1px solid; margin-right:400px; padding:10px;">
<div id="pimg"></div>
<div id="pid"></div>
<div id="name"></div>
<div id="gender"></div>
<div id="email"></div>
<div id="rstatus"></div>
<div id="purl"></div>
</div>
</div>
</body>
<script type="text/javascript">
var helper = (function() {
var authResult = undefined;
return {
onSignInCallback: function(authResult) {
if (authResult['access_token'])
{
this.authResult = authResult;
helper.connectServer();
gapi.client.load('plus','v1',this.renderProfile);
}
else if (authResult['error'])
{
console.log('There was an error: ' + authResult['error']);
$('#authOps').hide();
$('#gConnect').show();
}
console.log('authResult', authResult);
},
renderProfile: function()
{
gapi.client.load('oauth2', 'v2', function() {
gapi.client.oauth2.userinfo.get().execute(function(resp) {
// Shows user email
$('#email').append(
$('<p>Email : ' + resp.email + '</p>'));
})
});
var request = gapi.client.plus.people.get( {'userId' : 'me'} );
request.execute( function(profile) {
$('#pimg').empty();
$('#pid').empty();
$('#name').empty();
$('#purl').empty();
$('#gender').empty();
$('#rstatus').empty();
if (profile.error)
{
$('#profile').append(profile.error);
return;
}
$('#pimg').append($('<p><img style="margin:10px 10px 10px 10px;" src=\"' + profile.image.url + '\"></p>'));
$('#name').append($('<p>Name : '+ profile.displayName +'</p>' ));
$('#purl').append($('<p> Profile : '+ profile.url +'</p>' ));
$('#pid').append($('<p> Id : '+ profile.id +'</p>' ));
});
gapi.client.load('plus', 'v1', function() {
gapi.client.plus.people.get( {'userId' : 'me'} ).execute(function(resp) {
// Shows profile information
$('#gender').append($('<p>Gender : '+ resp.gender +'</p>' ));
$('#rstatus').append($('<p>Relationship Status: '+ resp.relationshipStatus +'</p>' ));
})
});
$('#authOps').show();
$('#gConnect').hide();
},
/**
* Calls the server endpoint to disconnect the app for the user.
*/
disconnectServer: function()
{
// Revoke the server tokens
$.ajax({
type: 'POST',
url: window.location.href + '/disconnect',
async: false,
success: function(result) {
console.log('revoke response: ' + result);
$('#authOps').hide();
$('#profile').empty();
$('#visiblePeople').empty();
$('#gConnect').show();
},
error: function(e) {
console.log(e);
}
});
},
connectServer: function()
{
console.log(this.authResult.code);
$.ajax({
type: 'POST',
url: window.location.href + '/connect?state={{ STATE }}',
contentType: 'application/octet-stream; charset=utf-8',
success: function(result) {
console.log(result);
helper.people();
},
processData: false,
data: this.authResult.code
});
},
people: function()
{
$.ajax({
type: 'GET',
url: window.location.href + '/people',
contentType: 'application/octet-stream; charset=utf-8',
success: function(result) {
helper.appendCircled(result);
},
processData: false
});
},
appendCircled: function(people) {
$('#visiblePeople').empty();
$('#visiblePeople').append('Number of people visible to this app: ' +
people.totalItems + '<br/>');
for (var personIndex in people.items) {
person = people.items[personIndex];
$('#visiblePeople').append('<img src="' + person.image.url + '">');
}
},
};
})();
$(document).ready(function() {
$('#disconnect').click(helper.disconnectServer);
if ($('[data-clientid="YOUR_CLIENT_ID"]').length > 0) {
alert('This sample requires your OAuth credentials (client ID) ' +
'from the Google APIs console:\n' +
' https://code.google.com/apis/console/#:access\n\n' +
'Find and replace YOUR_CLIENT_ID with your client ID and ' +
'YOUR_CLIENT_SECRET with your client secret in the project sources.'
);
}
});
function onSignInCallback(authResult) {
helper.onSignInCallback(authResult);
}
</script>
</html>