Ok gang, I have been searching the interwebz up and down trying to find a solid solution for xAuth and Appcelerator that isn't hyper-specific to Twitter.
I want to use Tumblr as the example as it is a very nice blogging platform with limited apps in the market place.
Additionally, if we can all work together and get a sweet template built for generic xAuth access I will throw it up on the Appcelerator marketplace for everyone to grab. (I'll include creds of all those who helped ofcourse!)
A quick background before the code - I have looked through multiple sets of sources, including but not limited to:
http://www.irlgaming.com/blog/oauth-xauth-appcelerator-titanium-%E2%80%93-solution/
http://developer.appcelerator.com/question/4581/custom-module
http://code.google.com/p/oauth-adapter/
The following code is a sort of hodge-podge mix of all the solutions I have found. I feel like I am very close and at this point I honestly need someone smarter then me with a fresh set of eyes to take a look.
So, lets get the collaboration going!
Complete gist is here: https://gist.github.com/1263082
Code is in three files: xauth_request.js, oauth.js and sha1.js
xauth_request.js:
/**************** OAUTH INCLUDES *******************/ Ti.include("oauth.js"); Ti.include("sha1.js"); /**************** OAUTH INCLUDES *******************/ //Get Tumblr App Key and Secret saved earlier //Note: These Key/Secret combo must be xAuth enabled and can be gained here: http://www.tumblr.com/oauth/apps var appKey = Titanium.App.Properties.getString('app_key'); var appSecret = Titanium.App.Properties.getString('app_secret'); //Get user info grabbed from log in form var Xusername = Titanium.App.Properties.getString('usernameVal'); var Xpassword = Titanium.App.Properties.getString('passwordVal'); //URL encode them just to be sure var urlEncodUserName = escape(Xusername); var urlEncodPassword = escape(Xpassword); var urlEncodAppKey = escape(appKey); var urlEncodAppSec = escape(appSecret); //Function as it is fired: // xAuthRequest('POST','https://www.tumblr.com/oauth/access_token', null); function xAuthRequest(method, url, params){ Ti.API.debug(method, url, params); var reqParams =[ ['oauth_consumer_key', urlEncodAppKey], ['oauth_nonce', OAuth.nonce(6)], ['oauth_signature_method', "HMAC-SHA1"], ['oauth_timestamp', OAuth.timestamp()], ['oauth_version', '1.0'], ['x_auth_mode', 'client_auth'], ['x_auth_password', urlEncodPassword], ['x_auth_username', urlEncodUserName] ]; if(params != null){ for (var x in params){ if(params[x].constructor == Array){ var paramData = params[x].length; for(i = 0; i < paramData; i++){ Ti.API.debug(x); Ti.API.debug(params[x][i]); reqParams.push([x+"["+i+"]",params[x][i]]) } } else { //not array reqParams.push([x,params[x]]) } } } var requestUrl = url+"?"; var accessor = {consumerSecret:appSecret+'&',tokenSecret: appSecret+'&'}; var message = { method: method, action: requestUrl, parameters: reqParams }; OAuth.completeRequest(message, accessor); var realm = 'https://www.tumblr.com/' var header = 'OAuth '; var list = OAuth.getParameterList(message.parameters); for (var p = 0; p < list.length; ++p) { var parameter = list[p]; var name = parameter[0]; if (name.indexOf("oauth_") == 0) { header += OAuth.percentEncode(name) + '="' + OAuth.percentEncode(parameter[1]) + '",'; } } //INFO for console Ti.API.info('LIST: '+list); Ti.API.info('header: '+header); var parameterMap = OAuth.getParameterMap(message.parameters) for (var p in parameterMap) { Ti.API.debug(p + ': ' + parameterMap[p]); requestUrl += "&" + p + "=" + parameterMap[p]; } Ti.API.debug("Request "+requestUrl); var xhr = Titanium.Network.createHTTPClient(); xhr.onreadystatechange = function(){ Titanium.API.debug(this.status +":"+ this.connected+":"+this.HEADERS_RECEIVED+":"+this.connectionType); } xhr.onload = function(){ //parse response into separate vars try{ var reply = JSON.parse(this.responseText); } catch(err){ var reply = this.responseText } //Tell console the reply Ti.API.info('onload reply: ' + reply); }; xhr.onerror = function (e) { Ti.API.info('on error reply in xauth: ' + JSON.stringify(e)); }; xhr.setRequestHeader('Authorization', header); xhr.open(method, requestUrl); xhr.send(); }
Thank you all in advance!
Again, the complete gist with all files can be found here: https://gist.github.com/1263082
Really excited to get this working.
Cheers, //MD