PlayBook OAuth Example with the Twitter API


/ Published in: ActionScript 3
Save to your folder(s)

An example of getting and updating a status with the Twitter API and OAuth for the BlackBerry PlayBook and ActionScript 3.


Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.display.Sprite;
  4. import flash.display.StageAlign;
  5. import flash.display.StageScaleMode;
  6. import flash.events.Event;
  7. import flash.events.HTTPStatusEvent;
  8. import flash.events.MouseEvent;
  9. import flash.net.URLLoader;
  10. import flash.net.URLLoaderDataFormat;
  11. import flash.net.URLRequest;
  12. import flash.net.URLRequestMethod;
  13. import flash.net.URLVariables;
  14. import flash.net.navigateToURL;
  15. import flash.text.Font;
  16. import flash.text.TextField;
  17. import flash.text.TextFieldType;
  18. import flash.text.TextFormat;
  19. import flash.text.TextFormatAlign;
  20.  
  21. import org.iotashan.oauth.OAuthConsumer;
  22. import org.iotashan.oauth.OAuthRequest;
  23. import org.iotashan.oauth.OAuthSignatureMethod_HMAC_SHA1;
  24. import org.iotashan.oauth.OAuthToken;
  25. import org.iotashan.utils.OAuthUtil;
  26. import org.iotashan.utils.URLEncoding;
  27.  
  28. import qnx.ui.buttons.LabelButton;
  29. import qnx.ui.core.Container;
  30. import qnx.ui.core.ContainerAlign;
  31. import qnx.ui.core.ContainerFlow;
  32. import qnx.ui.core.Containment;
  33. import qnx.ui.core.SizeMode;
  34. import qnx.ui.core.SizeUnit;
  35. import qnx.ui.core.Spacer;
  36. import qnx.ui.data.DataProvider;
  37. import qnx.ui.listClasses.AlternatingCellRenderer;
  38. import qnx.ui.listClasses.List;
  39. import qnx.ui.text.Label;
  40. import qnx.ui.text.TextInput;
  41.  
  42. public class PlayBookOauthDemo extends Sprite
  43. {
  44. // Static variables for OAuth
  45. private static var CONSUMER_SECRET:String = "<YOUR CONSUMER SECRET>";
  46. private static var CONSUMER_KEY:String = "<YOUR CONSUMER KEY>";
  47. private static var REQUEST_TOKEN_URL:String = "https://api.twitter.com/oauth/request_token";
  48. private static var ACCESS_TOKEN_URL:String = "https://api.twitter.com/oauth/access_token";
  49. private static var AUTHORIZE_URL:String = "https://api.twitter.com/oauth/authorize";
  50. private static var API_URL:String = "https://api.twitter.com";
  51. private static var SIGNATURE:OAuthSignatureMethod_HMAC_SHA1 = new OAuthSignatureMethod_HMAC_SHA1();
  52.  
  53. // Consumers and Request objects for making calls
  54. private var _consumer:OAuthConsumer;
  55. private var _authRequest:OAuthRequest;
  56. private var _accessRequest:OAuthRequest;
  57.  
  58.  
  59. // Tokens for passing information back and forth
  60. private var _requestToken:OAuthToken;
  61. private var _accessToken:OAuthToken;
  62.  
  63. // Containers to hold the UI objects
  64. private var _loginContainer:Container;
  65. private var _authContainer:Container;
  66. private var _verifyContainer:Container;
  67. private var _mainContainer:Container;
  68. private var _spacer:Spacer = new Spacer(50);
  69.  
  70. // PIN and Twitter text fields
  71. private var text:TextField;
  72. private var twitterTextField:TextField;
  73.  
  74. public function PlayBookOauthDemo()
  75. {
  76. super();
  77.  
  78. // support autoOrients
  79. stage.align = StageAlign.TOP_LEFT;
  80. stage.scaleMode = StageScaleMode.NO_SCALE;
  81.  
  82. init();
  83. }
  84.  
  85. protected function init():void
  86. {
  87. // creating the consumer and the authRequest based on the info from Twitter
  88. _consumer = new OAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
  89. _authRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_GET,REQUEST_TOKEN_URL,null,_consumer);
  90.  
  91. _loginContainer = new Container();
  92. _loginContainer.align = ContainerAlign.MID;
  93.  
  94. var button:LabelButton = new LabelButton();
  95. button.label = "Login to Twitter";
  96. button.addEventListener(MouseEvent.CLICK,onClick);
  97.  
  98.  
  99. _loginContainer.addChild(_spacer);
  100. _loginContainer.addChild(button);
  101. _loginContainer.setSize(1024,600);
  102. addChild(_loginContainer);
  103.  
  104. }
  105.  
  106. protected function onClick(event:MouseEvent):void
  107. {
  108. // uses the buildRequest method of our authRequest to format it correctly
  109. var urlRequest:URLRequest = new URLRequest(_authRequest.buildRequest(SIGNATURE));
  110. var loader:URLLoader = new URLLoader(urlRequest);
  111. loader.addEventListener(Event.COMPLETE,onRequestComplete);
  112. }
  113.  
  114. protected function onRequestComplete(event:Event):void
  115. {
  116. // build our requestToken based on the response from Twitter
  117. _requestToken = OAuthUtil.getTokenFromResponse(event.currentTarget.data);
  118.  
  119. _authContainer = new Container();
  120. _authContainer.align = ContainerAlign.MID;
  121.  
  122.  
  123. var authBtn:LabelButton = new LabelButton();
  124. authBtn.label = "Authorize this application";
  125. authBtn.addEventListener(MouseEvent.CLICK,onAuthClick);
  126.  
  127. _authContainer.addChild(_spacer);
  128. _authContainer.addChild(authBtn);
  129. _authContainer.setSize(1024,600);
  130.  
  131. removeChild(_loginContainer);
  132. addChild(_authContainer);
  133. }
  134.  
  135. protected function onAuthClick(event:MouseEvent):void
  136. {
  137. _verifyContainer = new Container();
  138. _verifyContainer.align = ContainerAlign.MID;
  139.  
  140. var label:Label = new Label();
  141. label.text = "Enter the PIN from Twitter.com";
  142.  
  143. var font:TextFormat = new TextFormat();
  144. font.align = TextFormatAlign.CENTER;
  145. font.bold = true;
  146. font.size = 24;
  147.  
  148. text = new TextField();
  149. text.type = TextFieldType.INPUT;
  150. text.border = true;
  151. text.width = 250;
  152. text.height = 30;
  153. text.defaultTextFormat = font;
  154.  
  155.  
  156. var getDataBtn:LabelButton = new LabelButton();
  157. getDataBtn.label = "Get Tweets";
  158. getDataBtn.addEventListener(MouseEvent.CLICK,onGetDataClick);
  159.  
  160. _verifyContainer.addChild(_spacer);
  161. _verifyContainer.addChild(label);
  162. _verifyContainer.addChild(text);
  163. _verifyContainer.addChild(getDataBtn);
  164. _verifyContainer.setSize(1024,600);
  165.  
  166.  
  167. removeChild(_authContainer);
  168. addChild(_verifyContainer);
  169.  
  170. // Use the key from the requestToken and send the user to Twitter
  171. // to Authorize our application
  172. var authRequest:URLRequest = new URLRequest('http://api.twitter.com/oauth/authorize?oauth_token='+_requestToken.key);
  173. navigateToURL(authRequest);
  174. }
  175.  
  176. protected function onGetDataClick(event:MouseEvent):void
  177. {
  178. // The oauth_verifier is the PIN number the user entered
  179. var params:Object = new Object();
  180. params.oauth_verifier = text.text;
  181.  
  182. // Create the access request
  183. _accessRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_GET,ACCESS_TOKEN_URL,params,_consumer,_requestToken);
  184.  
  185. var accessUrlRequest:URLRequest = new URLRequest(_accessRequest.buildRequest(SIGNATURE));
  186. var accessLoader:URLLoader = new URLLoader(accessUrlRequest);
  187. accessLoader.addEventListener(Event.COMPLETE,onAccessRequestComplete);
  188. }
  189.  
  190. protected function onAccessRequestComplete(event:Event):void
  191. {
  192. _accessToken = OAuthUtil.getTokenFromResponse(event.currentTarget.data);
  193.  
  194. var mainRequest:OAuthRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_GET,API_URL+'/1/statuses/friends_timeline.xml',null,_consumer,_accessToken);
  195.  
  196. var getStatusURLRequest:URLRequest = new URLRequest(mainRequest.buildRequest(SIGNATURE));
  197. var getStatusLoader:URLLoader = new URLLoader(getStatusURLRequest);
  198. getStatusLoader.addEventListener(Event.COMPLETE,onStatusLoadComplete);
  199. }
  200.  
  201. protected function onStatusLoadComplete(event:Event):void
  202. {
  203. _mainContainer = new Container();
  204. _mainContainer.flow = ContainerFlow.HORIZONTAL;
  205.  
  206. var sendTweetContainer:Container = new Container(25);
  207. sendTweetContainer.containment = Containment.DOCK_TOP;
  208.  
  209. var font:TextFormat = new TextFormat();
  210. font.align = TextFormatAlign.CENTER;
  211. font.bold = true;
  212. font.size = 24;
  213.  
  214. twitterTextField = new TextField();
  215. twitterTextField.type = TextFieldType.INPUT;
  216. twitterTextField.border = true;
  217. twitterTextField.width = 500;
  218. twitterTextField.height = 30;
  219. twitterTextField.defaultTextFormat = font;
  220.  
  221. var tweetLabel:LabelButton = new LabelButton();
  222. tweetLabel.label = "Tweet This";
  223. tweetLabel.addEventListener(MouseEvent.CLICK,onTweetClick);
  224.  
  225. sendTweetContainer.addChild(twitterTextField);
  226. sendTweetContainer.addChild(tweetLabel);
  227.  
  228. // Code for parsing the XML from the response
  229. var xml:XML = new XML(event.currentTarget.data);
  230. var statusList:XMLList = xml.children();
  231. var arr:Array = new Array();
  232.  
  233. for(var i:int=0;i<statusList.length();i++)
  234. {
  235. var obj:Object = new Object();
  236. obj.label = statusList[i].user.name.toString() +': ' + statusList[i].text.toString();
  237. arr.push(obj);
  238. }
  239.  
  240. // Create the DataProvider out of the parsed data
  241. var dataProvider:DataProvider = new DataProvider(arr);
  242. var list:List = new List();
  243. list.dataProvider = dataProvider;
  244. list.size = 100;
  245. list.sizeUnit = SizeUnit.PERCENT;
  246. list.setSkin(AlternatingCellRenderer);
  247.  
  248.  
  249. _mainContainer.addChild(list);
  250. _mainContainer.addChild(sendTweetContainer);
  251. _mainContainer.setSize(1024,600);
  252. removeChild(_verifyContainer);
  253. addChild(_mainContainer);
  254. }
  255.  
  256. protected function onTweetClick(event:MouseEvent):void
  257. {
  258. var params:Object = new Object();
  259. params.status = twitterTextField.text;
  260.  
  261. // Use the same consuemr and accessToken to update the Status
  262. var tweetRequest:OAuthRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_POST,API_URL+'/1/statuses/update.json',params,_consumer,_accessToken);
  263.  
  264. var setStatusURLRequest:URLRequest = new URLRequest(tweetRequest.buildRequest(SIGNATURE));
  265. setStatusURLRequest.method = URLRequestMethod.POST;
  266.  
  267. // use the replace function to strip out the status
  268. setStatusURLRequest.url = setStatusURLRequest.url.replace("&status=" + URLEncoding.encode(params.status),"");
  269.  
  270. // Add the status as a URLVariable since it's a POST operation
  271. setStatusURLRequest.data = new URLVariables( "status=" + twitterTextField.text );
  272.  
  273. var setStatusLoader:URLLoader = new URLLoader(setStatusURLRequest);
  274. setStatusLoader.addEventListener(Event.COMPLETE,onSetStatusComplete);
  275. }
  276.  
  277. protected function onSetStatusComplete(event:Event):void
  278. {
  279. // Show 'Status Updated' in the text field
  280. twitterTextField.text = "Status Updated";
  281. }
  282.  
  283.  
  284.  
  285. }
  286. }

URL: http://blog.digitalbackcountry.com/2011/02/using-oauth-for-twitter-authentication-on-the-blackberry-playbook/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.