Skip to content

Instantly share code, notes, and snippets.

@jeffbonnes
Created September 16, 2011 05:50
Show Gist options
  • Save jeffbonnes/1221301 to your computer and use it in GitHub Desktop.
Save jeffbonnes/1221301 to your computer and use it in GitHub Desktop.
Google Reader Starter
Titanium.UI.setBackgroundColor('#fff');
Ti.include( "js/greader.js");
var win = Ti.UI.createWindow({
});
var button1 = Ti.UI.createButton({
top:5,
height:30,
width:100,
title:'login',
left:5
});
button1.addEventListener('click', function(e){
greader.login( "YOUREMAIL" , "YOURPASSWORD" );
});
win.add(button1);
var button2 = Ti.UI.createButton({
top:5,
height:30,
width:100,
title:'get feeds',
left:115,
enabled: false
});
button2.addEventListener('click', function(e){
greader.getFeeds();
});
win.add(button2);
var label = Ti.UI.createLabel({
top : 5,
left: 220,
height: 30,
text:"Click the button to get started"
});
win.add( label );
var table = Ti.UI.createTableView({
top : 45
});
win.add(table)
Ti.App.addEventListener('greader.logincomplete', function(e){
Ti.API.debug('Google Reader Login complete' );
if( e.success ){
label.text = "login successful";
button2.enabled = true;
} else {
label.text = e.message;
}
});
Ti.App.addEventListener('greader.feeds', function(e){
if( e.success ){
label.text = "feeds fetched successfully";
Ti.API.debug( e.data );
var subs = [];
for( var i = 0; i < e.data.subscriptions.length ; i++ ){
subs.push( {title : e.data.subscriptions[i].title });
}
table.setData( subs );
} else {
label.text = e.message;
}
});
win.open();
var greader = {}; ( function() {
var cookie = "";
var GOOGLE = "http://www.google.com";
var GOOGLE_SSL = GOOGLE.replace("http://", "https://");
var LOGIN_URL = "/accounts/ClientLogin?service=reader";
var xhr = Ti.Network.createHTTPClient();
greader.login = function(email, password) {
// reset the cookie
cookie = "";
var url = GOOGLE_SSL + LOGIN_URL + "&Email=" + email + "&Passwd=" + password;
xhr.open("GET", url);
xhr.onload = function() {
var success = false;
var message = "";
if(xhr.status == "200") {
var response = xhr.responseText.split("\n");
for(var i = 0; i < response.length; i++) {
Ti.API.debug("greader:checking " + response[i]);
var tokens = response[i].split("=");
if(tokens[0] == "Auth") {
cookie = "GoogleLogin auth=" + tokens[1];
success = true;
message = "OK";
break;
}
}
if(!success) {
message = "cannot find Auth token";
Ti.API.debug("cannot find Auth token");
} else {
Ti.API.debug("greader:Auth is " + cookie);
}
} else if(this.status == "403") {
Ti.API.debug("greader:invalid credentials for google reader");
message = "invalid login";
} else {
Ti.API.debug("greader:unexpected status from google reader: " + this.status);
message = "unexpected status from google reader: " + this.status;
}
Ti.App.fireEvent('greader.logincomplete', {
success : success,
message : message
});
};
xhr.onerror = function() {
Ti.API.error("greader:error in google reader login");
Ti.App.fireEvent('greader.logincomplete', {
success : false,
message : "error in google reader login"
});
};
xhr.send();
};
greader.getFeeds = function() {
var url = "/reader/api/0/subscription/list?output=json";
xhr.open("GET", GOOGLE + url);
xhr.setRequestHeader("Authorization", cookie);
xhr.onload = function() {
Ti.App.fireEvent('greader.feeds', {
success : (xhr.status == "200"),
status : xhr.status,
data : JSON.parse(xhr.responseText)
});
};
xhr.send();
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment