public
Last active

  • Download Gist
TwitterExample.mxml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
<?xml version="1.0" encoding="utf-8"?>
<!-- Example app for github.com/srohde/Twitter -->
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
xmlns:twitter="com.soenkerohde.twitter.*"
currentState="initial"
creationComplete="creationCompleteHandler(event)">
 
<fx:Declarations>
<s:TraceTarget />
<!-- TODO Enter your Twitter Consumer Key/Secret -->
<twitter:Twitter id="twitter"
consumerKey=""
consumerSecret="" />
</fx:Declarations>
 
<fx:Script>
<![CDATA[
import com.soenkerohde.twitter.event.TwitterOAuthEvent;
import com.soenkerohde.twitter.event.TwitterStatusEvent;
import com.soenkerohde.twitter.event.TwitterUserEvent;
import flash.events.MouseEvent;
import flash.net.SharedObject;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.managers.CursorManager;
import org.iotashan.oauth.OAuthToken;
private var accessToken:OAuthToken;
[Bindable]
private var pinPending:Boolean = false;
[Bindable]
private var statusPending:Boolean = false;
[Bindable]
private var screenName:String;
protected function creationCompleteHandler( event : FlexEvent ) : void {
authenticate();
}
private function authenticate() : void {
var so:SharedObject = SharedObject.getLocal( "twitter" );
var token:Object = so.data["accessToken"];
// user has already an AccessToken
if ( token != null ) {
CursorManager.setBusyCursor();
accessToken = new OAuthToken( token.key, token.secret );
verifyAccessToken( accessToken );
// user is not authenticated yet
} else {
currentState = "unauthenticated";
twitter.authenticate();
}
}
protected function verifyAccessToken( token : OAuthToken ) : void {
twitter.addEventListener( TwitterUserEvent.USER_INFO, userInfoHandler );
twitter.addEventListener( TwitterUserEvent.USER_ERROR, userErrorHandler );
twitter.verifyAccessToken( token );
}
private function userInfoHandler( event : TwitterUserEvent ) : void {
currentState = "authenticated";
CursorManager.removeBusyCursor();
screenName = event.screenName;
twitter.removeEventListener( TwitterUserEvent.USER_INFO, userInfoHandler );
twitter.removeEventListener( TwitterUserEvent.USER_ERROR, userErrorHandler );
}
private function userErrorHandler( event : TwitterUserEvent ) : void {
currentState = "unauthenticated";
CursorManager.removeBusyCursor();
twitter.removeEventListener( TwitterUserEvent.USER_INFO, userInfoHandler );
twitter.removeEventListener( TwitterUserEvent.USER_ERROR, userErrorHandler );
}
protected function pinClickHandler( event : MouseEvent ) : void {
pinPending = true;
CursorManager.setBusyCursor();
twitter.addEventListener( TwitterOAuthEvent.ACCESS_TOKEN, accessTokenHandler );
twitter.obtainAccessToken( int( pin.text ) );
}
private function accessTokenHandler( event : TwitterOAuthEvent ) : void {
var so:SharedObject = SharedObject.getLocal( "twitter" );
so.data["accessToken"] = event.token;
so.flush();
currentState = "authenticated";
pinPending = false;
CursorManager.removeBusyCursor();
twitter.removeEventListener( TwitterOAuthEvent.ACCESS_TOKEN, accessTokenHandler );
verifyAccessToken( event.token );
}
protected function statusClickHandler( event : MouseEvent ) : void {
statusPending = true;
CursorManager.setBusyCursor();
twitter.addEventListener( TwitterStatusEvent.STATUS_SEND, statusSendHandler );
twitter.setStatus( accessToken, statusMessage.text );
}
private function statusSendHandler( event : TwitterStatusEvent ) : void {
Alert.show( "Your message was successfully sent.", "Status Updated" );
statusPending = false;
statusMessage.text = "";
CursorManager.removeBusyCursor();
twitter.removeEventListener( TwitterStatusEvent.STATUS_SEND, statusSendHandler );
}
protected function logoutClickHandler( event : MouseEvent ) : void {
var so:SharedObject = SharedObject.getLocal( "twitter" );
so.data["accessToken"] = null;
so.flush();
currentState = "unauthenticated";
authenticate();
}
]]>
</fx:Script>
 
<s:states>
<s:State name="initial" />
<s:State name="unauthenticated" />
<s:State name="authenticated" />
</s:states>
 
<s:Label text="Loading"
verticalCenter="0"
horizontalCenter="0"
includeIn="initial" />
 
<s:Group includeIn="unauthenticated"
horizontalCenter="0"
verticalCenter="0">
<s:layout>
<s:HorizontalLayout verticalAlign="middle" />
</s:layout>
<s:Label text="PIN" />
<s:TextInput id="pin"
enabled="{!pinPending}"
restrict="0-9" />
<s:Button label="Validate PIN"
enabled="{!pinPending &amp;&amp; pin.text != ''}"
click="pinClickHandler(event)" />
</s:Group>
 
<s:Group includeIn="authenticated"
horizontalCenter="0"
verticalCenter="0">
<s:layout>
<s:VerticalLayout />
</s:layout>
<s:Label text="Hello {screenName}" />
<s:Label text="Set Status"
enabled="{!statusPending}" />
<s:TextArea id="statusMessage" />
<s:Button label="Send"
enabled="{!statusPending &amp;&amp; statusMessage.text != ''}"
click="statusClickHandler(event)" />
<s:Button label="Logout"
click="logoutClickHandler(event)" />
</s:Group>
 
</s:WindowedApplication>

Thanks for the Post.
I see many example talks only about the first 3 steps of request,access, authorize token. But no example beyond that. (May be I am poor to search from last 1 month).
Can any one share how to use these library for sending request after these requests. Posting request gets 401 each time.
Please help!!!

Hi, I'm trying to use the example above, but the pin page never appears to me. Debugging the OAuth lib, I've notice in the code below pasted from the authenticate function I'd never reach the handler. Is that some config in my app on twitter, maybe the callback url? Because when I've copied the request and paste in a browser seems to be authenticated ok. Any idea? Thanks in advance.
var loader:URLLoader = new URLLoader( request );
loader.addEventListener( Event.COMPLETE, requestTokenHandler );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.