Permalink
Browse files

+ Google+ Post fetching

  • Loading branch information...
1 parent 138e53f commit a421c6e8deb95e4ed5176845330450b6f49d4068 @timdream committed Sep 17, 2011
Showing with 192 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +79 −0 go2.js
  3. +37 −0 jquery.getcontent.js
  4. +75 −0 tests/jquery.getcontent.html
View
@@ -4,3 +4,4 @@ wordcloud.min.js
wordfreq.min.js
wordfreq.worker.min.js
worker.min.js
+go2.min.js
View
@@ -0,0 +1,79 @@
+// Simple Google API OAuth 2.0 Client flow lib
+// Get access token and pass to callback
+
+"use strict";
+
+(function (w) {
+
+ if (window.opener && window.opener.GO2) {
+ if (window.location.hash.indexOf('access_token')) {
+ window.opener.GO2.receiveToken(
+ window.location.hash.replace(/^.*access_token=([^&]+).*$/, '$1'),
+ parseInt(window.location.hash.replace(/^.*expires_in=([^&]+).*$/, '$1'))
+ );
+ window.close();
+ }
+
+ if (window.location.search.indexOf('error=')) {
+ window.opener.GO2.receiveToken('ERROR');
+ window.close();
+ }
+ }
+
+ var client_id,
+ scope = 'https://www.googleapis.com/auth/plus.me',
+ redirect_uri = window.location.href.substr(0, window.location.href.length - window.location.hash.length),
+ access_token,
+ callbackWaitForToken;
+
+ w.GO2 = {
+ // init
+ init: function (f_client_id, f_scope, f_redirect_uri) {
+ if (!f_client_id) return false;
+ if (f_scope) scope = f_scope;
+ client_id = f_client_id;
+ },
+ // receive token from popup
+ receiveToken: function (token, expires_in) {
+ if (token !== 'ERROR') {
+ access_token = token;
+ if (callbackWaitForToken) callbackWaitForToken(access_token);
+ setTimeout(
+ function () {
+ access_token = undefined;
+ },
+ expires_in * 1000
+ );
+ } else if (token === false) {
+ callbackWaitForToken = undefined;
+ }
+ },
+ // boolean, indicate logged in or not
+ isLoggedIn: function () {
+ return !!access_token;
+ },
+ // pass the access token to callback
+ // if not logged in this triggers login popup;
+ // use isLoggedIn to check login first to prevent popup blocker
+ getToken: function (callback) {
+ if (!client_id || !redirect_uri || !scope) {
+ alert('You need init() first. Check the program flow.');
+ return false;
+ }
+ if (!access_token) {
+ callbackWaitForToken = callback;
+ window.open(
+ 'https://accounts.google.com/o/oauth2/auth'
+ + '?client_id=' + client_id
+ + '&redirect_uri=' + redirect_uri
+ + '&scope=' + scope
+ + '&response_type=token',
+ 'Google OAuth 2.0 Login window',
+ 'width=400,height=360'
+ );
+ } else {
+ return callback(access_token);
+ }
+ }
+ };
+})(this);
View
@@ -6,6 +6,8 @@ $.getContent = function (source, options) {
type: 'auto',
beforeComplete: $.noop,
complete: $.noop,
+ googleOAuthKey: '',
+ googleAPIKey: '',
encoding: 'UTF-8', // Text encoding
num: -1, // feed num
timeout: 25 * 1000
@@ -119,6 +121,38 @@ $.getContent = function (source, options) {
}
);
},
+ getGooglePlusText = function () {
+ if (source === 'me' && settings.googleOAuthKey === '') complete(''); // 'me' need key
+ var url = 'https://www.googleapis.com/plus/v1/people/' + source + '/activities/public?maxResults=100&alt=json&pp=1&callback=?';
+ if (settings.googleAPIKey) url += '&key=' + settings.googleAPIKey;
+ if (settings.googleOAuthKey) url += '&access_token=' + settings.googleOAuthKey;
+
+ $.getJSON(
+ url,
+ function (data, status) {
+ if (data.error || !data.title) {
+ complete('');
+ return;
+ }
+
+ beforeComplete(data.title);
+ var text = [];
+ data.items.forEach(
+ function (item) {
+ text.push(item.object.content.replace(/<[^>]+?>|\(.+?\.\.\.\)|\&\w+\;|<script.+?\/script\>/ig, ''));
+ }
+ );
+
+ text = text.join('\n');
+ setTimeout(
+ function () {
+ complete(text);
+ },
+ 0
+ );
+ }
+ );
+ },
parseYQLElementObject = function (text, obj) {
// TBD, properly exclude script
for (var key in obj) if (obj.hasOwnProperty(key)) {
@@ -205,6 +239,9 @@ $.getContent = function (source, options) {
case 'facebook':
return getFbText();
break;
+ case 'googleplus':
+ return getGooglePlusText();
+ break;
default:
complete('');
break;
@@ -66,20 +66,45 @@
<input type="submit" value="test" />
</p>
</form>
+<h2>$.getContent(googleplus:me);</h2>
+<form method="get" action="#" id="googleplus">
+ <p>
+ <div id='fb-root'></div>
+ <input type="submit" value="test" />
+ </p>
+</form>
+<h2>$.getContent(googleplus:userid);</h2>
+<form method="get" action="#" id="googleplus_id">
+ <p>
+ <label>ID: <input type="text" id="googleplus_id_userid" size="20" value="106189723444098348646" /></label>
+ <input type="submit" value="test" />
+ </p>
+</form>
<textarea id="result" readonly="true" cols="80" rows="30">
</textarea>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="../jquery.getcontent.js"></script>
+<script type="text/javascript" src="../go2.js"></script>
<script type="text/javascript">
var FB_app_id = '';
+var googleAPIKey = '';
+var googleClientId = '';
jQuery(function ($) {
if (!FB_app_id) {
alert('Add your own Facebook App ID to enable Facebook status fetching.');
}
+
+ if (!googleAPIKey) {
+ alert('Add your own Google API Key to enable Google+ posts fetching.');
+ }
+
+ if (!googleClientId) {
+ alert('Add your own Google Client Id to enable private Google+ posts fetching.');
+ }
FB.init({
appId : FB_app_id,
@@ -88,6 +113,10 @@
xfbml: true
});
+ GO2.init(
+ googleClientId
+ );
+
$('#feedSupported').addClass(($.getContent.feedSupported?'':'not_') + 'support');
$('#htmlSupported').addClass(($.getContent.htmlSupported?'':'not_') + 'support');
$('#fileSupported').addClass(($.getContent.fileSupported?'':'not_') + 'support');
@@ -252,6 +281,52 @@
start();
}
+ $('#googleplus').bind(
+ 'submit',
+ function () {
+ GO2.getToken(
+ test_googleplus
+ );
+
+ return false;
+ }
+ );
+
+ function test_googleplus(token) {
+ try {
+ $.getContent(
+ 'me',
+ {
+ type: 'googleplus',
+ beforeComplete: beforeComplete,
+ complete: complete,
+ googleOAuthKey: token//,
+ //googleAPIKey: googleAPIKey
+ }
+ );
+ } catch (e) {error(e); }
+ start();
+ return false;
+ };
+
+ $('#googleplus_id').bind(
+ 'submit',
+ function () {
+ try {
+ $.getContent(
+ $('#googleplus_id_userid').val(),
+ {
+ type: 'googleplus',
+ beforeComplete: beforeComplete,
+ complete: complete,
+ googleAPIKey: googleAPIKey
+ }
+ );
+ } catch (e) {error(e); }
+ start();
+ return false;
+ }
+ );
});
</script>
</body>

0 comments on commit a421c6e

Please sign in to comment.