Return to Snippet

Revision: 50170
at August 13, 2011 08:23 by ryanstewart


Initial Code
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
		xmlns:s="library://ns.adobe.com/flex/spark"
		backgroundAlpha="0" actionBarVisible="false"
		title="TwitterAuthView" 
		add="view1_addHandler(event)"
		creationComplete="view1_creationCompleteHandler(event)"
		removing="view1_removingHandler(event)">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			import org.iotashan.oauth.OAuthConsumer;
			import org.iotashan.oauth.OAuthRequest;
			import org.iotashan.oauth.OAuthSignatureMethod_HMAC_SHA1;
			import org.iotashan.oauth.OAuthToken;
			import org.iotashan.utils.OAuthUtil;
			
			import spark.events.ViewNavigatorEvent;
			
			import vo.Profile;
			
			private var consumerKey:String = "your consumer key";
			private var consumerSecret:String = "your consumer secret";
			private var twitterRequestURL:String = "https://api.twitter.com/oauth/request_token";
			private var twitterAuthURL:String = "https://api.twitter.com/oauth/authorize";
			private var twitterTokenURL:String = "https://api.twitter.com/oauth/access_token";
			private var requestToken:OAuthToken;
			
			private var twitterWebView:StageWebView;
			private var accessRequest:OAuthRequest;

			private var consumer:OAuthConsumer;
			private var accessToken:OAuthToken;
			
			private var thisProfile:Profile;
			
			override public function set data(value:Object):void 
			{
				thisProfile = value as Profile;
			}		
			
			override public function createReturnObject():Object
			{
				return thisProfile;
			}
			
			protected function view1_addHandler(event:FlexEvent):void
			{
				if(navigator.poppedViewReturnedObject)
				{
					thisProfile = navigator.poppedViewReturnedObject.object as Profile;
				}
			}				
						
			
			protected function view1_creationCompleteHandler(event:FlexEvent):void
			{
				consumer = new OAuthConsumer(consumerKey,consumerSecret);
				
				var oauth:OAuthRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_GET,twitterRequestURL,null,consumer);
				var request:URLRequest = new URLRequest(oauth.buildRequest(new OAuthSignatureMethod_HMAC_SHA1()));
				var loader:URLLoader = new URLLoader(request);
				loader.addEventListener(Event.COMPLETE,onLoaderComplete);
			}			
						
			protected function onLoadComplete(event:Event):void
			{
				//				if(twitterWebView.location == 'https://api.twitter.com/oauth/authorize')
				//				{
				//					twitterWebView.loadURL("javascript:var myFunction="
				//						+ "function(){document.location=document.body.innerHTML;}");
				//					twitterWebView.loadURL("javascript:myFunction();");					
				//				}
				//				twitterWebView.loadURL("javascript:var myFunction="
				//					+ "function(){document.location=document.body.innerHTML;}");
				//				twitterWebView.loadURL("javascript:myFunction();");
				trace(twitterWebView.location);
			}
			
			protected function onLocationChanging(event:LocationChangeEvent):void
			{	
				trace(event.location);
				//				trace('test');
			}
			
			protected function onLoaderComplete(event:Event):void
			{
				var baseHeight:int = actionBar.height + twitterInstructions.height + tiPinNumber.height + btnFinished.height+25;
				
				requestToken = OAuthUtil.getTokenFromResponse(event.currentTarget.data);
				var authRequest:URLRequest = new URLRequest('http://api.twitter.com/oauth/authorize?oauth_token='+requestToken.key);
				
				twitterWebView = new StageWebView();
				twitterWebView.viewPort = new Rectangle(0,baseHeight,stage.width,stage.height-baseHeight);
				twitterWebView.stage = this.stage;
				twitterWebView.assignFocus();
				twitterWebView.loadURL(authRequest.url);
				twitterWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGING,onLocationChanging);
				twitterWebView.addEventListener(Event.COMPLETE,onLoadComplete);				
			}			
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				var params:Object = new Object();
					params.oauth_verifier = tiPinNumber.text;
					
				accessRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_GET,twitterTokenURL,params,consumer,requestToken);
				var accessUrlRequest:URLRequest = new URLRequest(accessRequest.buildRequest(new OAuthSignatureMethod_HMAC_SHA1()));
				var accessLoader:URLLoader = new URLLoader(accessUrlRequest);
					accessLoader.addEventListener(Event.COMPLETE,onAccessRequestComplete);
//				navigator.popView();
			}
			
			protected function onAccessRequestComplete(event:Event):void
			{
				accessToken = OAuthUtil.getTokenFromResponse(event.currentTarget.data);
				thisProfile.twitterAccessKey = accessToken.key;
				thisProfile.twitterAccessSecret = accessToken.secret;
				twitterWebView.dispose();
				navigator.pushView(ConfigureSocialNetworksView,thisProfile);
			}
			
			protected function view1_removingHandler(event:ViewNavigatorEvent):void
			{
				twitterWebView.dispose();
			}
			
		]]>
	</fx:Script>
	<s:layout>
		<s:VerticalLayout horizontalAlign="center" />
	</s:layout>
	<s:ActionBar id="actionBar" skinClass="skins.MyActionBarSkin" width="100%" />
	<fx:Style source="styles/MainStyle.css" />		
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<s:Label id="twitterInstructions" width="90%" styleName="LabelFont" 
			 text="Log into twitter in the box below, then authorize the application and put the PIN number in the box below." />
	<s:TextInput id="tiPinNumber" width="90%" skinClass="skins.TextInputSkin" styleName="LabelFont" />
	<s:Button id="btnFinished" label="Finished" click="button1_clickHandler(event)" styleName="ButtonLabel" 
			  width="90%" skinClass="skins.ContinueButtonSkin" />
</s:View>

Initial URL

                                

Initial Description
Authenticating against Twitter within a Flex Mobile View. I do all of it within a StageWebView window so I can have them type the pin into the app while seeing it.

Initial Title
Twitter Authentication with Flex Mobile

Initial Tags
twitter

Initial Language
ActionScript 3