2013-08-27 42 views
0

因此,我正在開發一個Android應用程序(在使用Adobe AIR的AS3中),該應用程序應列出公共Twitter帳戶的時間表。 我從來沒有任何開發使用互聯網請求的應用程序的經驗,什麼不是。在AS3 AIR應用程序中獲取Twitter時間線

我也聽說twitter最近改變了他們的api。

任何機構能幫助我嗎?如何獲取json或xml格式的非受保護帳戶的時間線?以及認證如何在AS3中工作?

在此先感謝

回答

1

https://github.com/susisu/Twitter-for-AS3 使用,做工精細,據我可以測試。

如果你需要它,這是我實現的例子,可能有點幫助:

package com.company.social { 

    import com.company.AppConst; 
    import com.company.IDestroyable; 
    import com.company.Main; 
    import com.company.displayassets.WebViewCloseStripe; 
    import com.company.events.TwitterControllerEvent; 

    import flash.display.BitmapData; 
    import flash.display.PNGEncoderOptions; 
    import flash.display.Sprite; 
    import flash.display.Stage; 
    import flash.events.Event; 
    import flash.events.EventDispatcher; 
    import flash.events.IOErrorEvent; 
    import flash.events.LocationChangeEvent; 
    import flash.events.MouseEvent; 
    import flash.events.SecurityErrorEvent; 
    import flash.filesystem.File; 
    import flash.filesystem.FileMode; 
    import flash.filesystem.FileStream; 
    import flash.geom.Rectangle; 
    import flash.media.StageWebView; 
    import flash.net.URLRequest; 
    import flash.utils.ByteArray; 
    import flash.utils.clearInterval; 
    import flash.utils.clearTimeout; 
    import flash.utils.setInterval; 
    import flash.utils.setTimeout; 

    import isle.susisu.twitter.Twitter; 
    import isle.susisu.twitter.TwitterRequest; 
    import isle.susisu.twitter.TwitterTokenSet; 
    import isle.susisu.twitter.events.TwitterErrorEvent; 
    import isle.susisu.twitter.events.TwitterRequestEvent; 

    public class TwitterController extends EventDispatcher implements IDestroyable { 

     private var _destroyed:Boolean; 
     private var _twitter:Twitter; 
     private var _token:TwitterTokenSet; 
     private var _twitterReq:TwitterRequest; 
     private var _stage:Stage; 
     private var _topStripe:WebViewCloseStripe; 
     private var _webView:StageWebView; 
     private var _pinExtractable:Boolean; 
     private var _activity:String; 
     private var _timeoutID:uint; 
     private var _pinTimeoutID:uint; 
     private static const ACTIVITY_LOGIN:String = "login"; 
     private static const ACTIVITY_TWEET:String = "tweet"; 
     private static const FILENAME:String = AppConst.TWITTER_FILE; 
     private static const CONSUMER_KEY:String = AppConst.TWITTER_CONSUMER_KEY; 
     private static const CONSUMER_SECRET:String = AppConst.TWITTER_CONSUMER_SECRET; 

     public function TwitterController(stage:Stage) { 
      _stage = stage; 
      init(); 
     } 

     private function init():void { 
      var complete:Function; 

      checkForSettings(); 

      _activity = ACTIVITY_LOGIN; 

      if (_token && _token.oauthToken.length) { 
       trace("token"); 
       _twitter = new Twitter(CONSUMER_KEY, CONSUMER_SECRET, _token.oauthToken, _token.oauthTokenSecret); 
       _twitterReq = _twitter.account_verifyCredentials(); 
       complete = verifyCompleteHandler; 
      } 
      else { 
       trace("no token"); 
       _twitter = new Twitter(CONSUMER_KEY, CONSUMER_SECRET); 
       _twitterReq = _twitter.oauth_requestToken(); 
       complete = tokenCompleteHandler; 
      } 

      setHandlersForRequest(complete); 
      startTimeout(); 
     } 

     private function startTimeout():void { 
      trace("timeout start"); 
      clearTimeout(_timeoutID); 
      _timeoutID = setTimeout(timeout, AppConst.TIMEOUT_TIME); 
     } 

     private function timeout():void { 
      trace("timed out"); 
      clearTimeout(_timeoutID); 
      if(_twitterReq) { 
       _twitterReq.removeEventListener(TwitterRequestEvent.COMPLETE, tokenCompleteHandler); 
       _twitterReq.removeEventListener(TwitterRequestEvent.COMPLETE, verifyCompleteHandler); 
       _twitterReq.removeEventListener(TwitterRequestEvent.COMPLETE, pinRequestCompleteHandler); 
       _twitterReq.removeEventListener(TwitterRequestEvent.COMPLETE, tweetCompleteHandler); 

       _twitterReq.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
       _twitterReq.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler); 
       _twitterReq.removeEventListener(TwitterErrorEvent.CLIENT_ERROR, errorHandler); 
       _twitterReq.removeEventListener(TwitterErrorEvent.SERVER_ERROR, errorHandler); 
      } 
      dispatchEvent(new TwitterControllerEvent(TwitterControllerEvent.TIMEOUT)); 
     } 

     private function stopTimeout():void { 
      trace("timeout stop"); 
      clearTimeout(_timeoutID); 
     } 

     private function setHandlersForRequest(completeHandler:Function):void { 
      trace("set"); 
      _twitterReq.addEventListener(TwitterRequestEvent.COMPLETE, completeHandler); 
      _twitterReq.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
      _twitterReq.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler); 
      _twitterReq.addEventListener(TwitterErrorEvent.CLIENT_ERROR, errorHandler); 
      _twitterReq.addEventListener(TwitterErrorEvent.SERVER_ERROR, errorHandler); 
     } 

     private function removeHandlersForRequest(completeHandler:Function):void { 
      trace("remove"); 
      _twitterReq.removeEventListener(TwitterRequestEvent.COMPLETE, completeHandler); 
      _twitterReq.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
      _twitterReq.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler); 
      _twitterReq.removeEventListener(TwitterErrorEvent.CLIENT_ERROR, errorHandler); 
      _twitterReq.removeEventListener(TwitterErrorEvent.SERVER_ERROR, errorHandler); 
     } 

     private function verifyCompleteHandler(e:TwitterRequestEvent):void { 
      stopTimeout(); 
      removeHandlersForRequest(verifyCompleteHandler); 
      dispatchEvent(new TwitterControllerEvent(TwitterControllerEvent.LOGIN_COMPLETE)); 
     } 

     private function tokenCompleteHandler(e:TwitterRequestEvent):void { 
      stopTimeout(); 
      removeHandlersForRequest(tokenCompleteHandler); 

      var request:URLRequest = new URLRequest(_twitter.getOAuthAuthorizeURL()); 

      _topStripe = new WebViewCloseStripe(); 
      _topStripe.getCloseButton().addEventListener(MouseEvent.CLICK, closeClickHandler); 
      _stage.addChild(_topStripe); 

      _webView = new StageWebView(); 
      _webView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, locationChangeHandler); 
      _webView.addEventListener(Event.COMPLETE, locationCompleteHandler); 
      _webView.viewPort = new Rectangle(0, _topStripe.height, _stage.fullScreenWidth, _stage.fullScreenHeight - _topStripe.height); 
      _webView.stage = _stage; 
      _webView.assignFocus(); 
      _webView.loadURL(request.url); 
     } 

     private function locationChangeHandler(e:LocationChangeEvent):void { 
      if (e.location.search("oauth_token") >= 0) { 
       trace("loc change outath_token, returning: "+e.location); 
       return; 
      } 
      else { 
       _pinExtractable = true; 
       trace("the magic should happen here: "+e.location); 
      } 
     } 

     private function locationCompleteHandler(e:Event):void { 
      if(_pinExtractable) { 
       _webView.loadURL("javascript:document.title=document.documentElement.innerHTML;"); 
       _pinTimeoutID = setTimeout(extractPin, 16); 
      } 
     } 

     private function extractPin():void { 
      clearTimeout(_pinTimeoutID); 
      var pin:String = String(_webView.title.split('<kbd aria-labelledby="code-desc"><code>')[1]).split("<")[0]; 
      trace("pin: "+pin); 

      if (pin && pin.toString() != "undefined") { 
       _webView.removeEventListener(LocationChangeEvent.LOCATION_CHANGE, locationChangeHandler); 
       _webView.removeEventListener(Event.COMPLETE, locationCompleteHandler); 
       _webView.dispose(); 
       _webView = null; 

       _topStripe.getCloseButton().removeEventListener(MouseEvent.CLICK, closeClickHandler); 
       _topStripe.destroy(); 
       _stage.removeChild(_topStripe); 
       _topStripe = null; 

       _twitterReq = _twitter.oauth_accessToken(pin); 
       //request.addEventListener(TwitterRequestEvent.COMPLETE, pinRequestCompleteHandler); 
       setHandlersForRequest(pinRequestCompleteHandler); 
       startTimeout(); 
      } 
      else { 
       _pinTimeoutID = setTimeout(extractPin, 100); 
      } 
     } 

     private function pinRequestCompleteHandler(e:Event):void { 
      stopTimeout(); 
      removeHandlersForRequest(pinRequestCompleteHandler); 

      _token = _twitter.accessTokenSet; 

      saveSettings(); 

      dispatchEvent(new TwitterControllerEvent(TwitterControllerEvent.LOGIN_COMPLETE)); 

      trace("tokens: "+_token.oauthToken, _token.oauthTokenSecret); 
     } 

     private function checkForSettings():void { 
      var file:File = File.applicationStorageDirectory.resolvePath(FILENAME); 
      var fileStream:FileStream = new FileStream(); 

      if(file.exists) { 
       fileStream.open(file, FileMode.READ); 
       var data:Object = fileStream.readObject() as Object; 
       fileStream.close(); 

       _token = new TwitterTokenSet(CONSUMER_KEY, CONSUMER_SECRET, data.oauthToken, data.oauthTokenSecret); 
      } 
      else { 
       _token = null 
      } 
     } 

     private function saveSettings():void { 
      var file:File = File.applicationStorageDirectory.resolvePath(FILENAME); 
      var fileStream:FileStream = new FileStream(); 
      fileStream.open(file, FileMode.WRITE); 
      fileStream.writeObject({oauthToken: _token.oauthToken, oauthTokenSecret: _token.oauthTokenSecret}); 
      fileStream.close(); 
     } 

     private function errorHandler(e:Event):void { 
      var request:TwitterRequest = e.currentTarget as TwitterRequest; 

      request.removeEventListener(TwitterRequestEvent.COMPLETE, tokenCompleteHandler); 
      request.removeEventListener(TwitterRequestEvent.COMPLETE, verifyCompleteHandler); 
      request.removeEventListener(TwitterRequestEvent.COMPLETE, pinRequestCompleteHandler); 
      request.removeEventListener(TwitterRequestEvent.COMPLETE, tweetCompleteHandler); 

      request.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
      request.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler); 
      request.removeEventListener(TwitterErrorEvent.CLIENT_ERROR, errorHandler); 
      request.removeEventListener(TwitterErrorEvent.SERVER_ERROR, errorHandler); 

      if(_activity == ACTIVITY_LOGIN) { 
       dispatchEvent(new TwitterControllerEvent(TwitterControllerEvent.LOGIN_ERROR)); 
      } 
      else if(_activity == ACTIVITY_TWEET) { 
       dispatchEvent(new TwitterControllerEvent(TwitterControllerEvent.TWEET_ERROR)); 
      } 

      if(e is TwitterErrorEvent) { 
       trace(TwitterErrorEvent(e).statusCode.toString()); 
      } 
     } 

     private function tweetCompleteHandler(e:Event):void { 
      trace("Tweet complete."); 
      stopTimeout(); 
      removeHandlersForRequest(tweetCompleteHandler); 
      dispatchEvent(new TwitterControllerEvent(TwitterControllerEvent.TWEET_COMPLETE)); 
     } 

     private function closeClickHandler(e:MouseEvent):void { 
      dispatchEvent(new TwitterControllerEvent(TwitterControllerEvent.CLOSE)); 
     } 

     public function reset():void { 
      _token = new TwitterTokenSet("", "", "", ""); 
      saveSettings(); 
      dispatchEvent(new TwitterControllerEvent(TwitterControllerEvent.RESET)); 
     } 

     public function tweet(message:String):void { 
      startTimeout(); 
      _activity = ACTIVITY_TWEET; 
      _twitterReq = _twitter.statuses_update(message); 
      setHandlersForRequest(tweetCompleteHandler); 
     } 

     public function tweetWithImage(message:String, imageData:BitmapData):void { 
      startTimeout(); 
      var byteArray:ByteArray = imageData.encode(new Rectangle(0, 0, imageData.width, imageData.height), new PNGEncoderOptions()); 
      _twitterReq = _twitter.statuses_updateWithMedia(message, byteArray); 
      setHandlersForRequest(tweetCompleteHandler); 
     } 

     public function destroy():void { 
      if (_destroyed) return; 

      clearTimeout(_pinTimeoutID); 

      if(_twitterReq) { 
       _twitterReq.removeEventListener(TwitterRequestEvent.COMPLETE, tokenCompleteHandler); 
       _twitterReq.removeEventListener(TwitterRequestEvent.COMPLETE, verifyCompleteHandler); 
       _twitterReq.removeEventListener(TwitterRequestEvent.COMPLETE, pinRequestCompleteHandler); 
       _twitterReq.removeEventListener(TwitterRequestEvent.COMPLETE, tweetCompleteHandler); 

       _twitterReq.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
       _twitterReq.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler); 
       _twitterReq.removeEventListener(TwitterErrorEvent.CLIENT_ERROR, errorHandler); 
       _twitterReq.removeEventListener(TwitterErrorEvent.SERVER_ERROR, errorHandler); 
      } 
      _twitterReq = null; 

      if(_webView) { 
       _webView.removeEventListener(LocationChangeEvent.LOCATION_CHANGE, locationChangeHandler); 
       _webView.removeEventListener(Event.COMPLETE, locationCompleteHandler); 
       _webView.dispose(); 
       _webView = null; 
      } 

      if(_topStripe) { 
       _topStripe.getCloseButton().removeEventListener(MouseEvent.CLICK, closeClickHandler); 
       _topStripe.destroy(); 
       _stage.removeChild(_topStripe); 
       _topStripe = null; 
      } 

      _stage = null; 
      if(_twitter) _twitter = null; 
      if(_token) _token = null; 

      _destroyed = true; 
     } 

     public function isDestroyed():Boolean { 
      return _destroyed; 
     } 
    } 
} 
+0

如何使用的HTMLLoader做到這一點? –

+0

com.company資產在哪裏? –

+0

@eco_bach這些資產不必感興趣,因爲它們只是間接相關的(它們實際上與我提到的庫的實現沒有多大關係),而且它們不言自明(例如:WebViewCloseStripe.getCloseButton將顯然意味着它是一個「關閉按鈕精靈/ mc」; AppConst.TWITTER_CONSUMER_KEY =>我想不需要評論這些,等等) – Fygo

相關問題