Skip to content

Instantly share code, notes, and snippets.

@srohde
Created December 30, 2009 16:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save srohde/266163 to your computer and use it in GitHub Desktop.
Save srohde/266163 to your computer and use it in GitHub Desktop.
<?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>
@sharma4rajesh
Copy link

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!!!

@webverse
Copy link

webverse commented Mar 5, 2012

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 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment