使用Flex Mobile进行Twitter身份验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Flex Mobile进行Twitter身份验证相关的知识,希望对你有一定的参考价值。

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.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
  3. xmlns:s="library://ns.adobe.com/flex/spark"
  4. backgroundAlpha="0" actionBarVisible="false"
  5. title="TwitterAuthView"
  6. add="view1_addHandler(event)"
  7. creationComplete="view1_creationCompleteHandler(event)"
  8. removing="view1_removingHandler(event)">
  9. <fx:Script>
  10. <![CDATA[
  11. import mx.events.FlexEvent;
  12.  
  13. import org.iotashan.oauth.OAuthConsumer;
  14. import org.iotashan.oauth.OAuthRequest;
  15. import org.iotashan.oauth.OAuthSignatureMethod_HMAC_SHA1;
  16. import org.iotashan.oauth.OAuthToken;
  17. import org.iotashan.utils.OAuthUtil;
  18.  
  19. import spark.events.ViewNavigatorEvent;
  20.  
  21. import vo.Profile;
  22.  
  23. private var consumerKey:String = "your consumer key";
  24. private var consumerSecret:String = "your consumer secret";
  25. private var twitterRequestURL:String = "https://api.twitter.com/oauth/request_token";
  26. private var twitterAuthURL:String = "https://api.twitter.com/oauth/authorize";
  27. private var twitterTokenURL:String = "https://api.twitter.com/oauth/access_token";
  28. private var requestToken:OAuthToken;
  29.  
  30. private var twitterWebView:StageWebView;
  31. private var accessRequest:OAuthRequest;
  32.  
  33. private var consumer:OAuthConsumer;
  34. private var accessToken:OAuthToken;
  35.  
  36. private var thisProfile:Profile;
  37.  
  38. override public function set data(value:Object):void
  39. {
  40. thisProfile = value as Profile;
  41. }
  42.  
  43. override public function createReturnObject():Object
  44. {
  45. return thisProfile;
  46. }
  47.  
  48. protected function view1_addHandler(event:FlexEvent):void
  49. {
  50. if(navigator.poppedViewReturnedObject)
  51. {
  52. thisProfile = navigator.poppedViewReturnedObject.object as Profile;
  53. }
  54. }
  55.  
  56.  
  57. protected function view1_creationCompleteHandler(event:FlexEvent):void
  58. {
  59. consumer = new OAuthConsumer(consumerKey,consumerSecret);
  60.  
  61. var oauth:OAuthRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_GET,twitterRequestURL,null,consumer);
  62. var request:URLRequest = new URLRequest(oauth.buildRequest(new OAuthSignatureMethod_HMAC_SHA1()));
  63. var loader:URLLoader = new URLLoader(request);
  64. loader.addEventListener(Event.COMPLETE,onLoaderComplete);
  65. }
  66.  
  67. protected function onLoadComplete(event:Event):void
  68. {
  69. // if(twitterWebView.location == 'https://api.twitter.com/oauth/authorize')
  70. // {
  71. // twitterWebView.loadURL("javascript:var myFunction="
  72. // + "function(){document.location=document.body.innerhtml;}");
  73. // twitterWebView.loadURL("javascript:myFunction();");
  74. // }
  75. // twitterWebView.loadURL("javascript:var myFunction="
  76. // + "function(){document.location=document.body.innerHTML;}");
  77. // twitterWebView.loadURL("javascript:myFunction();");
  78. trace(twitterWebView.location);
  79. }
  80.  
  81. protected function onLocationChanging(event:LocationChangeEvent):void
  82. {
  83. trace(event.location);
  84. // trace('test');
  85. }
  86.  
  87. protected function onLoaderComplete(event:Event):void
  88. {
  89. var baseHeight:int = actionBar.height + twitterInstructions.height + tiPinNumber.height + btnFinished.height+25;
  90.  
  91. requestToken = OAuthUtil.getTokenFromResponse(event.currentTarget.data);
  92. var authRequest:URLRequest = new URLRequest('http://api.twitter.com/oauth/authorize?oauth_token='+requestToken.key);
  93.  
  94. twitterWebView = new StageWebView();
  95. twitterWebView.viewPort = new Rectangle(0,baseHeight,stage.width,stage.height-baseHeight);
  96. twitterWebView.stage = this.stage;
  97. twitterWebView.assignFocus();
  98. twitterWebView.loadURL(authRequest.url);
  99. twitterWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGING,onLocationChanging);
  100. twitterWebView.addEventListener(Event.COMPLETE,onLoadComplete);
  101. }
  102.  
  103. protected function button1_clickHandler(event:MouseEvent):void
  104. {
  105. var params:Object = new Object();
  106. params.oauth_verifier = tiPinNumber.text;
  107.  
  108. accessRequest = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_GET,twitterTokenURL,params,consumer,requestToken);
  109. var accessUrlRequest:URLRequest = new URLRequest(accessRequest.buildRequest(new OAuthSignatureMethod_HMAC_SHA1()));
  110. var accessLoader:URLLoader = new URLLoader(accessUrlRequest);
  111. accessLoader.addEventListener(Event.COMPLETE,onAccessRequestComplete);
  112. // navigator.popView();
  113. }
  114.  
  115. protected function onAccessRequestComplete(event:Event):void
  116. {
  117. accessToken = OAuthUtil.getTokenFromResponse(event.currentTarget.data);
  118. thisProfile.twitterAccessKey = accessToken.key;
  119. thisProfile.twitterAccessSecret = accessToken.secret;
  120. twitterWebView.dispose();
  121. navigator.pushView(ConfigureSocialNetworksView,thisProfile);
  122. }
  123.  
  124. protected function view1_removingHandler(event:ViewNavigatorEvent):void
  125. {
  126. twitterWebView.dispose();
  127. }
  128.  
  129. ]]>
  130. </fx:Script>
  131. <s:layout>
  132. <s:VerticalLayout horizontalAlign="center" />
  133. </s:layout>
  134. <s:ActionBar id="actionBar" skinClass="skins.MyActionBarSkin" width="100%" />
  135. <fx:Style source="styles/MainStyle.css" />
  136. <fx:Declarations>
  137. <!-- Place non-visual elements (e.g., services, value objects) here -->
  138. </fx:Declarations>
  139. <s:Label id="twitterInstructions" width="90%" styleName="LabelFont"
  140. text="Log into twitter in the box below, then authorize the application and put the PIN number in the box below." />
  141. <s:TextInput id="tiPinNumber" width="90%" skinClass="skins.TextInputSkin" styleName="LabelFont" />
  142. <s:Button id="btnFinished" label="Finished" click="button1_clickHandler(event)" styleName="ButtonLabel"
  143. width="90%" skinClass="skins.ContinueButtonSkin" />
  144. </s:View>

以上是关于使用Flex Mobile进行Twitter身份验证的主要内容,如果未能解决你的问题,请参考以下文章

Twitter bootstrap flex 垂直中心不能使用 align-self-center

如何在 Twitter API 中使用获取帐户/设置?

在向 Twitter/Facebook 进行身份验证后向 Spring Security 进行身份验证

如何使用已安装的 Twitter 应用进行身份验证

使用 LINQ to Twitter 进行身份验证后获取用户信息

取消后再次进行身份验证时,Android Twitter Kit 崩溃