Skip to content

Instantly share code, notes, and snippets.

@kensch
Created August 19, 2011 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kensch/1157563 to your computer and use it in GitHub Desktop.
Save kensch/1157563 to your computer and use it in GitHub Desktop.
Pin Based auth using jsOAuth, jQuery and PhoneGap on webOS
<!DOCTYPE html>
<html>
<head>
<!--
This is a sample PhoneGap webOS application that uses the Meetup.com OAuth API,
demonstrating an initial request by PIN based authentication. It relies on jsOAuth
for the heavy lifting in authentication, PhoneGap for device and database support,
and somewhat unnecessarily on jQuery. The jQuery structure is just to implement a
pseudo-switching of pages. This is designed on webOS, but should work well across
platforms with some minor tweaks.
The code is based on a Github gist by Ed Finkler (@funkatron), which is based on a
blog post by Łukasz Korecki (@lukaszkorecki), which is all made possible by the
work on jsOAuth by Rob Griffiths (@bytespider).
Feel free to use this code as you see fit for commercial or non-commercial purposes.
Ken Schreihofer (@digitalelph)
-->
<meta charset="utf-8">
<title>jsOAuth+PhoneGap</title>
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript" src="jsOAuth-1.3.js"></script>
<script>
var oauth, requestParams, accessParams;
var db;
function clearBody(){
document.body.innerHTML="";
}
function initial() {
clearBody();
$('#body').append("<button id=\'launch\' onclick=\"launch()\">Launch</button>");
}
function launch() {
var options = {
consumerKey: 'YOURKEY',
consumerSecret: 'YOURSECRET',
requestTokenUrl: "https://api.meetup.com/oauth/request/",
authorizationUrl: "http://www.meetup.com/authenticate/",
accessTokenUrl: "https://api.meetup.com/oauth/access/"
};
oauth = OAuth(options);
oauth.fetchRequestToken(goodInitial, failInitial);
function goodInitial(data) {
console.dir("Good Initial Request: ");
navigator.window.newCard('http://www.meetup.com/authorize/?set_mobile=on&oauth_token='+
oauth.getAccessTokenKey()+'&oauth_token_secret='+oauth.getAccessTokenSecret()
+'&oauth_callback_confirmed=true');
clearBody();
$('#body').append("<input id=\"pin\" type=\"text\" value=\"\">");
$('#body').append("<button id=\'close\' onclick=\"window.close()\">Close</button>");
$('#body').append("<button id=\'pinbutton\' onclick=\"pinval()\">Save</button>");
$('#body').append("<div id=\"notice\"></div>");
requestParams = data.text;
}
function failInitial(data) {
console.dir("Bad Initial Request: "+data.text);
}
}
function pinval() {
if ($('#pin').val()) {
oauth.setVerifier($('#pin').val());
oauth.get('https://api.meetup.com/oauth/access?'+requestParams,
function(data) {
console.dir("Good Pin: "+data.text);
// split the query string as needed
var accessParams = {};
var qvars_tmp = data.text.split('&');
for (var i = 0; i < qvars_tmp.length; i++) {;
var y = qvars_tmp[i].split('=');
accessParams[y[0]] = decodeURIComponent(y[1]);
};
oauth.setAccessToken([accessParams.oauth_token, accessParams.oauth_token_secret]);
db = window.openDatabase("oauth", "1.0", "OAuth DB", false);
db.transaction(populateDB, errorCB, successCB);
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS OAUTH');
tx.executeSql('CREATE TABLE IF NOT EXISTS OAUTH (data)');
tx.executeSql('INSERT INTO OAUTH (data) VALUES ("'+oauth.getAccessToken()+'")');
}
function errorCB(tx, err) {
navigator.notification.alert("error: " + err);
}
function successCB() {
navigator.notification.alert('successfully created & populated');
db.transaction(queryDB, errorCB);
}
function queryDB(tx) {
tx.executeSql('SELECT * FROM OAUTH', [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
var len = results.rows.length;
navigator.notification.alert('rows inserted: ' + len);
if (len > 0) {
for (var i=0;i<len;i++) {
navigator.notification.alert(' data: ' + results.rows.item(i).data);
}
}
}
getHomelist(data);
},
function(data) {
console.dir("Bad Pin: "+data.text);
}
);
}
}
function getHomelist(datam) {
requestParams=datam.text;
console.dir("Request params"+requestParams);
oauth.getJSON('https://api.meetup.com/groups.json?'+requestParams,
function(data_object) {
console.dir("Good List"+data_object.text);
$('#notice').append($.map(data_object.results, function(t, i){ return '<p>' + t.name + '</p>'; }).join(''));
},
function(data) {
console.dir("Bad List: "+data.text); }
);
}
</script>
</head>
<body id="body" onload="navigator.device.deviceReady();">
<h1>Fresh Meet</h1>
<!--<button id='launch' onclick="launch()">Launch</button>
<input id="pin" type="text" value=""><button id='pinbutton' onclick="pinval()">Save</button>-->
<button id='initial' onclick="initial()">Initial</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment