Skip to content

Commit

Permalink
worked through adapter to find where destroy window was crashing on a…
Browse files Browse the repository at this point in the history
…ndroid, simplified window and removed modal behaviour.
  • Loading branch information
stereoket committed Mar 23, 2011
1 parent 3ea8562 commit 821da5a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 57 deletions.
69 changes: 29 additions & 40 deletions oauth_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,7 @@
// load the access token for the service (if previously saved)
oAuthAdapter.loadAccessToken('twitter');
// consume a service API - in this case the status update by Twitter
oAuthAdapter.send('https://api.twitter.com/1/statuses/update.json', [['status','Hey @ziodave, I managed to use the #oauth adapter for @titanium consuming @twitterapi']],'Twitter','Tweet published.','Tweet not published.');
// if the client is not authorized, ask for authorization. the previous tweet will be sent automatically after authorization
if (oAuthAdapter.isAuthorized() == false)
{
// this function will be called as soon as the application is authorized
var receivePin = function() {
// get the access token with the provided pin/oauth_verifier
oAuthAdapter.getAccessToken('https://api.twitter.com/oauth/access_token');
// save the access token
oAuthAdapter.saveAccessToken('twitter');
};
// show the authorization UI and call back the receive PIN function
oAuthAdapter.showAuthorizeUI('https://api.twitter.com/oauth/authorize?' + oAuthAdapter.getRequestToken('https://api.twitter.com/oauth/request_token'), receivePin);
}
// Please refer to the README for new usage and dependencies
*/
/*
Expand All @@ -65,13 +49,19 @@
* Save them locally in a lib subfolder
*/

// Modifications by Ketan Majmudar Spirit Quest (http://www.spiritquest.co.uk 2011)
// Github Repo: https://github.com/stereoket/oauth-adapter
// Have hacked this from multiple forks from the original I have also worked thorugh to attempt to make this android comaptible, there is more work to do, distilling this down into something that is cross compatible - if you would like to donate to the time spent making this compatible you can donate to http://goo.gl/h4UKN (pay pal link) or via my website http://www.stereoartist.com/projects.php



// create an OAuthAdapter instance
var OAuthAdapterNew = function(pConsumerSecret, pConsumerKey, pSignatureMethod){

Ti.API.info('*********************************************');
Ti.API.info('If you like the OAuth Adapter, consider donating at');
Ti.API.info('https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=T5HUU4J5EQTJU&lc=IT&item_name=OAuth%20Adapter&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted');
Ti.API.info('*** http://goo.gl/h4UKN (pay pal link) ***');
Ti.API.info('(also please consider daonting to David, link at top of source)');
Ti.API.info('*********************************************');

// will hold the consumer secret and consumer key as provided by the caller
Expand Down Expand Up @@ -280,7 +270,7 @@ var OAuthAdapterNew = function(pConsumerSecret, pConsumerKey, pSignatureMethod){
Ti.API.debug('destroyAuthorizeUI:webView.removeEventListener');
webView.removeEventListener('load', authorizeUICallback);
Ti.API.debug('destroyAuthorizeUI:window.close()');
window.hide();
window.hide();
} catch(ex) {
Ti.API.debug('Cannot destroy the authorize UI. Ignoring.');
}
Expand Down Expand Up @@ -319,6 +309,7 @@ var OAuthAdapterNew = function(pConsumerSecret, pConsumerKey, pSignatureMethod){
setTimeout(receivePinCallback, 100);
}
}
Ti.API.debug(Ti.UI.currentWindow.name);
return destroyAuthorizeUI();
}
if('https://api.twitter.com/oauth/authorize' == loc){
Expand All @@ -331,7 +322,12 @@ var OAuthAdapterNew = function(pConsumerSecret, pConsumerKey, pSignatureMethod){
Ti.API.debug('PIN: '+ pin);

if(pin != ''){
if (receivePinCallback) setTimeout(receivePinCallback, 100);
if (receivePinCallback) setTimeout(receivePinCallback, 300);
var osname = Ti.Platform.osname; // cache information
if(osname === 'android') {
window.close();
Ti.API.debug('Android Window destruction');
}
destroyAuthorizeUI();
}
}
Expand All @@ -340,7 +336,7 @@ var OAuthAdapterNew = function(pConsumerSecret, pConsumerKey, pSignatureMethod){
this.createWindow = function (){
var w = Ti.UI.createWindow({
top: 0,
modal: true,
// modal: true,
fullscreen: true
});
w.open();
Expand Down Expand Up @@ -376,29 +372,20 @@ var OAuthAdapterNew = function(pConsumerSecret, pConsumerKey, pSignatureMethod){

webView = Ti.UI.createWebView({
url: pUrl,
width: 310,
height: 400,
width: '100%',
height: '100%',
scalesPageToFit: true,
touchEnabled: true,
top: '20px',
top: 0,
border: 10,
backgroundColor: 'white',
borderColor: '#aaa',
borderRadius: 20,
borderWidth: 5,
// backgroundColor: 'white',
// borderColor: '#aaa',
// borderRadius: 20,
// borderWidth: 5,
});

webView.addEventListener('load', authorizeUICallback);
//view.add(webView);

this.setupWindow(webView);

/*
var animation = Ti.UI.createAnimation();
animation.transform = Ti.UI.create2DMatrix();
animation.duration = 500;
view.animate(animation);
*/
};

this.getAccessToken = function(params){
Expand Down Expand Up @@ -563,7 +550,7 @@ Ti.API.debug('access Tokens: ' + accessToken + ':' + accessTokenSecret);
}
var client = Ti.Network.createHTTPClient();
Ti.API.debug('Network client setup');
client.setRequestHeader('Content-Type',"application/x-www-form-urlencoded");

client.onerror = function(e){
Ti.API.debug('Error Found');
Ti.API.debug(e);
Expand Down Expand Up @@ -598,8 +585,10 @@ Ti.API.debug('access Tokens: ' + accessToken + ':' + accessTokenSecret);

Ti.API.debug('header = ' + header);
client.setRequestHeader('Authorization', header);
}

} else {
client.setRequestHeader('Content-Type',"application/x-www-form-urlencoded");
}

client.send();
Ti.API.debug('Request Sent');
return client.responseText;
Expand Down
41 changes: 24 additions & 17 deletions twitter_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ var oa={};
oa.consumerSecret = 'xxxxx';
oa.consumerKey = 'xxxxx';

oa.oAuthAdapter = new OAuthAdapterNew(
oa.consumerSecret,
oa.consumerKey,
'HMAC-SHA1');

})();

oa.oAuthAdapter = new OAuthAdapterNew(
oa.consumerSecret,
oa.consumerKey,
'HMAC-SHA1');

// load the access token for the service (if previously saved)
oa.oAuthAdapter.loadAccessToken('twitter');
if (oa.oAuthAdapter.isAuthorized() == false)
Expand All @@ -34,6 +36,7 @@ var oa={};

accessor.tokenSecret = '';
var message = oa.oAuthAdapter.createMessage('https://api.twitter.com/oauth/request_token', 'POST');
Ti.API.info('message is '+ message);
OAuth.setTimestampAndNonce(message);
OAuth.setParameter(message, "oauth_timestamp", OAuth.timestamp());
OAuth.SignatureMethod.sign(message, accessor);
Expand All @@ -43,26 +46,30 @@ var oa={};

client.onload = function() {
try {
var responseParams = OAuth.getParameterMap(client.responseText);
requestToken = responseParams.oauth_token;
requestTokenSecret = responseParams.oauth_token_secret;
Ti.API.debug(client.readyState);
Ti.API.debug(client.status);
this.token = client.responseText;
oa.authTokens = client.responseText;
var responseParams = OAuth.getParameterMap(oa.authTokens);
requestToken = responseParams.oauth_token;
requestTokenSecret = responseParams.oauth_token_secret;
Ti.API.debug('Ready State: '+client.readyState);
Ti.API.debug('Status: '+client.status);
Ti.API.debug('Reponse text: '+ oa.authTokens);
setTimeout(function()
{
Ti.API.debug(oa.authTokens);

// Ti.API.info('Request token from twitter.js settings: ' + client.responseText);
oa.oAuthAdapter.showAuthorizeUI('https://api.twitter.com/oauth/authorize?' + oa.authTokens, receivePin);
},4000);

} catch(e){
alert(E);
}
};
Ti.API.debug(finalUrl+ ' Is the finalURL for auth step 1');
client.open('POST', finalUrl, false);
client.setRequestHeader('X-Requested-With',null);
// client.setTimeout(3000);
client.send();
var requestTokens = client.responseText;
// have to use a settimeout function to allow ANDROID to get the return value to pass onto the next function.
setTimeout(function()
{
Ti.API.debug('Variable: '+ requestTokens);
Ti.API.info('Request token from twitter.js settings: ' + client.responseText);
oa.oAuthAdapter.showAuthorizeUI('https://api.twitter.com/oauth/authorize?' + client.responseText, receivePin);
},4000);

}

0 comments on commit 821da5a

Please sign in to comment.