ActionScript 3 使用Twitter API的PlayBook OAuth示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 使用Twitter API的PlayBook OAuth示例相关的知识,希望对你有一定的参考价值。

package
{	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	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 org.iotashan.utils.URLEncoding;
	
	import qnx.ui.buttons.LabelButton;
	import qnx.ui.core.Container;
	import qnx.ui.core.ContainerAlign;
	import qnx.ui.core.ContainerFlow;
	import qnx.ui.core.Containment;
	import qnx.ui.core.SizeMode;
	import qnx.ui.core.SizeUnit;
	import qnx.ui.core.Spacer;
	import qnx.ui.data.DataProvider;
	import qnx.ui.listClasses.AlternatingCellRenderer;
	import qnx.ui.listClasses.List;
	import qnx.ui.text.Label;
	import qnx.ui.text.TextInput;
	
	public class PlayBookOauthDemo extends Sprite
	{
		// Static variables for OAuth
		private static var CONSUMER_SECRET:String = "<YOUR CONSUMER SECRET>";
		private static var CONSUMER_KEY:String = "<YOUR CONSUMER KEY>";
		private static var REQUEST_TOKEN_URL:String = "https://api.twitter.com/oauth/request_token";
		private static var ACCESS_TOKEN_URL:String = "https://api.twitter.com/oauth/access_token";
		private static var AUTHORIZE_URL:String = "https://api.twitter.com/oauth/authorize";
		private static var API_URL:String = "https://api.twitter.com";
		private static var SIGNATURE:OAuthSignatureMethod_HMAC_SHA1 = new OAuthSignatureMethod_HMAC_SHA1();

		// Consumers and Request objects for making calls
		private var _consumer:OAuthConsumer;
		private var _authRequest:OAuthRequest;
		private var _accessRequest:OAuthRequest;
		
		
		// Tokens for passing information back and forth
		private var _requestToken:OAuthToken;
		private var _accessToken:OAuthToken;

		// Containers to hold the UI objects
		private var _loginContainer:Container;
		private var _authContainer:Container;
		private var _verifyContainer:Container;
		private var _mainContainer:Container;
		private var _spacer:Spacer = new Spacer(50);
		
		// PIN and Twitter text fields
		private var text:TextField;
		private var twitterTextField:TextField;
		
		public function PlayBookOauthDemo()
		{
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
				
			init();
		}
		
		protected function init():void
		{	
			// creating the consumer and the authRequest based on the info from Twitter
			_consumer = new OAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
			_authRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_GET,REQUEST_TOKEN_URL,null,_consumer);
			
			_loginContainer = new Container();
			_loginContainer.align = ContainerAlign.MID;

			var button:LabelButton = new LabelButton();
				button.label = "Login to Twitter";
				button.addEventListener(MouseEvent.CLICK,onClick);
			
			
			_loginContainer.addChild(_spacer);
			_loginContainer.addChild(button);
			_loginContainer.setSize(1024,600);
			addChild(_loginContainer);
			
		}
		
		protected function onClick(event:MouseEvent):void
		{
			// uses the buildRequest method of our authRequest to format it correctly
			var urlRequest:URLRequest = new URLRequest(_authRequest.buildRequest(SIGNATURE));
			var loader:URLLoader = new URLLoader(urlRequest);
				loader.addEventListener(Event.COMPLETE,onRequestComplete);
		}
		
		protected function onRequestComplete(event:Event):void
		{
			// build our requestToken based on the response from Twitter
			_requestToken = OAuthUtil.getTokenFromResponse(event.currentTarget.data);
			
			_authContainer = new Container();
			_authContainer.align = ContainerAlign.MID;
			
			
			var authBtn:LabelButton = new LabelButton();
				authBtn.label = "Authorize this application";
				authBtn.addEventListener(MouseEvent.CLICK,onAuthClick);

			_authContainer.addChild(_spacer);
			_authContainer.addChild(authBtn);
			_authContainer.setSize(1024,600);
			
			removeChild(_loginContainer);
			addChild(_authContainer);
		}	
		
		protected function onAuthClick(event:MouseEvent):void
		{
			_verifyContainer = new Container();
			_verifyContainer.align = ContainerAlign.MID;
			
			var label:Label = new Label();
				label.text = "Enter the PIN from Twitter.com";
			
			var font:TextFormat = new TextFormat();
				font.align = TextFormatAlign.CENTER;
				font.bold = true;
				font.size = 24;
			
			text = new TextField();
			text.type = TextFieldType.INPUT;
			text.border = true;
			text.width = 250;
			text.height = 30;
			text.defaultTextFormat = font;
			
			
			var getDataBtn:LabelButton = new LabelButton();
				getDataBtn.label = "Get Tweets";
				getDataBtn.addEventListener(MouseEvent.CLICK,onGetDataClick);
			
			_verifyContainer.addChild(_spacer);
			_verifyContainer.addChild(label);
			_verifyContainer.addChild(text);
			_verifyContainer.addChild(getDataBtn);
			_verifyContainer.setSize(1024,600);
			
			
			removeChild(_authContainer);
			addChild(_verifyContainer);
				
			// Use the key from the requestToken and send the user to Twitter
			// to Authorize our application
			var authRequest:URLRequest = new URLRequest('http://api.twitter.com/oauth/authorize?oauth_token='+_requestToken.key);
			navigateToURL(authRequest);
		}
		
		protected function onGetDataClick(event:MouseEvent):void
		{
			// The oauth_verifier is the PIN number the user entered
			var params:Object = new Object();
				params.oauth_verifier = text.text;
			
			// Create the access request
			_accessRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_GET,ACCESS_TOKEN_URL,params,_consumer,_requestToken);
			
			var accessUrlRequest:URLRequest = new URLRequest(_accessRequest.buildRequest(SIGNATURE));
			var accessLoader:URLLoader = new URLLoader(accessUrlRequest);
				accessLoader.addEventListener(Event.COMPLETE,onAccessRequestComplete);
		}
		
		protected function onAccessRequestComplete(event:Event):void
		{
			_accessToken = OAuthUtil.getTokenFromResponse(event.currentTarget.data);
			
			var mainRequest:OAuthRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_GET,API_URL+'/1/statuses/friends_timeline.xml',null,_consumer,_accessToken);
			
			var getStatusURLRequest:URLRequest = new URLRequest(mainRequest.buildRequest(SIGNATURE));
			var getStatusLoader:URLLoader = new URLLoader(getStatusURLRequest);
				getStatusLoader.addEventListener(Event.COMPLETE,onStatusLoadComplete);
		}
		
		protected function onStatusLoadComplete(event:Event):void
		{
			_mainContainer = new Container();
			_mainContainer.flow = ContainerFlow.HORIZONTAL;
		
			var sendTweetContainer:Container = new Container(25);
				sendTweetContainer.containment = Containment.DOCK_TOP;
			
			var font:TextFormat = new TextFormat();
				font.align = TextFormatAlign.CENTER;
				font.bold = true;
				font.size = 24;
			
			twitterTextField = new TextField();
			twitterTextField.type = TextFieldType.INPUT;
			twitterTextField.border = true;
			twitterTextField.width = 500;
			twitterTextField.height = 30;
			twitterTextField.defaultTextFormat = font;
				
			var tweetLabel:LabelButton = new LabelButton();
				tweetLabel.label = "Tweet This";
				tweetLabel.addEventListener(MouseEvent.CLICK,onTweetClick);
				
				sendTweetContainer.addChild(twitterTextField);
				sendTweetContainer.addChild(tweetLabel);
				
			// Code for parsing the XML from the response
			var xml:XML = new XML(event.currentTarget.data);
			var statusList:XMLList = xml.children();
			var arr:Array = new Array();

			for(var i:int=0;i<statusList.length();i++)
			{
				var obj:Object = new Object();
					obj.label = statusList[i].user.name.toString() +': ' + statusList[i].text.toString();
				arr.push(obj);
			}
			
			// Create the DataProvider out of the parsed data 
			var dataProvider:DataProvider = new DataProvider(arr);
			var list:List = new List();
				list.dataProvider = dataProvider;
				list.size = 100;
				list.sizeUnit = SizeUnit.PERCENT;
				list.setSkin(AlternatingCellRenderer);
			
				
			_mainContainer.addChild(list);
			_mainContainer.addChild(sendTweetContainer);
			_mainContainer.setSize(1024,600);
			removeChild(_verifyContainer);
			addChild(_mainContainer);
		}
		
		protected function onTweetClick(event:MouseEvent):void
		{
			var params:Object = new Object();
				params.status = twitterTextField.text;
			
			// Use the same consuemr and accessToken to update the Status
			var tweetRequest:OAuthRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_POST,API_URL+'/1/statuses/update.json',params,_consumer,_accessToken);
			
			var setStatusURLRequest:URLRequest = new URLRequest(tweetRequest.buildRequest(SIGNATURE));
				setStatusURLRequest.method = URLRequestMethod.POST;
				
				// use the replace function to strip out the status
				setStatusURLRequest.url = setStatusURLRequest.url.replace("&status=" + URLEncoding.encode(params.status),"");
			
			// Add the status as a URLVariable since it's a POST operation
			setStatusURLRequest.data = new URLVariables( "status=" + twitterTextField.text  );

			var setStatusLoader:URLLoader = new URLLoader(setStatusURLRequest);
				setStatusLoader.addEventListener(Event.COMPLETE,onSetStatusComplete);
		}		
		
		protected function onSetStatusComplete(event:Event):void
		{
			// Show 'Status Updated' in the text field
			twitterTextField.text = "Status Updated";
		}					

		
		
	}
}

以上是关于ActionScript 3 使用Twitter API的PlayBook OAuth示例的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 twitter服务请求

ActionScript 3 获取Twitter ATOM搜索Feed

ActionScript 3 Twitter推文字符串编码实体实用程序

ActionScript 3 在Twitter上分享(Bit.ly缩短链接)[FLEX]

ActionScript 3 使用ActionScript 3暂停框架

ActionScript 3 clickTag使用ActionScript 3