-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleplus.js
140 lines (140 loc) · 4.6 KB
/
googleplus.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
var auth2 = {};
var helper = (function() {
return {
/**
* Hides the sign in button and starts the post-authorization operations.
*
* @param {Object} authResult An Object which contains the access token and
* other authentication information.
*/
onSignInCallback: function(authResult) {
$('#authResult').html('Auth Result:<br/>');
for (var field in authResult) {
$('#authResult').append(' ' + field + ': ' +
authResult[field] + '<br/>');
}
if (authResult.isSignedIn.get()) {
$('#authOps').show('slow');
$('#gConnect').hide();
helper.profile();
helper.people();
} else {
if (authResult['error'] || authResult.currentUser.get().getAuthResponse() == null) {
// There was an error, which means the user is not signed in.
// As an example, you can handle by writing to the console:
console.log('There was an error: ' + authResult['error']);
}
$('#authResult').append('Logged out');
$('#authOps').hide('slow');
$('#gConnect').show();
}
console.log('authResult', authResult);
},
/**
* Calls the OAuth2 endpoint to disconnect the app for the user.
*/
disconnect: function() {
// Revoke the access token.
auth2.disconnect();
},
/**
* Gets and renders the list of people visible to this app.
*/
people: function() {
gapi.client.plus.people.list({
'userId': 'me',
'collection': 'visible'
}).then(function(res) {
var people = res.result;
$('#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 + '">');
}
});
},
/**
* Gets and renders the currently signed in user's profile data.
*/
profile: function(){
gapi.client.plus.people.get({
'userId': 'me'
}).then(function(res) {
var profile = res.result;
console.log(profile);
$('#profile').empty();
$('#profile').append(
$('<p><img src=\"' + profile.image.url + '\"></p>'));
$('#profile').append(
$('<p>Hello ' + profile.displayName + '!<br />Tagline: ' +
profile.tagline + '<br />About: ' + profile.aboutMe + '</p>'));
if (profile.emails) {
$('#profile').append('<br/>Emails: ');
for (var i=0; i < profile.emails.length; i++){
$('#profile').append(profile.emails[i].value).append(' ');
}
$('#profile').append('<br/>');
}
if (profile.cover && profile.coverPhoto) {
$('#profile').append(
$('<p><img src=\"' + profile.cover.coverPhoto.url + '\"></p>'));
}
}, function(err) {
var error = err.result;
$('#profile').empty();
$('#profile').append(error.message);
});
}
};
})();
/**
* jQuery initialization
*/
$(document).ready(function() {
$('#disconnect').click(helper.disconnect);
$('#loaderror').hide();
if ($('meta')[0].content == 'YOUR_CLIENT_ID') {
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.'
);
}
});
/**
* Handler for when the sign-in state changes.
*
* @param {boolean} isSignedIn The new signed in state.
*/
var updateSignIn = function() {
console.log('update sign in state');
if (auth2.isSignedIn.get()) {
console.log('signed in');
helper.onSignInCallback(gapi.auth2.getAuthInstance());
}else{
console.log('signed out');
helper.onSignInCallback(gapi.auth2.getAuthInstance());
}
}
/**
* This method sets up the sign-in listener after the client library loads.
*/
function startApp() {
gapi.load('auth2', function() {
gapi.client.load('plus','v1').then(function() {
gapi.signin2.render('signin-button', {
scope: 'https://www.googleapis.com/auth/plus.login',
fetch_basic_profile: false });
gapi.auth2.init({fetch_basic_profile: false,
scope:'https://www.googleapis.com/auth/plus.login'}).then(
function (){
console.log('init');
auth2 = gapi.auth2.getAuthInstance();
auth2.isSignedIn.listen(updateSignIn);
auth2.then(updateSignIn);
});
});
});
}