Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shi11/476155 to your computer and use it in GitHub Desktop.
Save shi11/476155 to your computer and use it in GitHub Desktop.
variation to srohde's example - uses Air.HTML window rather than a browser, and doesn't force the user to manually enter a pin following authentication.
<?xml version="1.0" encoding="utf-8"?>
<!-- Example app for github.com/srohde/Twitter -->
<s:WindowedApplication height="350" width="765"
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.*"
xmlns:mx1="library://ns.adobe.com/flex/mx"
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.addEventListener(TwitterOAuthEvent.REQUEST_TOKEN, onRequestToken);
htmlWindow.addEventListener(Event.LOCATION_CHANGE, onLocationChange);
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);
}
//GET THE ACTUAL OAUTH TOKEN
private function onRequestToken(event : TwitterOAuthEvent) : void
{
event.preventDefault();
htmlWindow.location = Twitter.AUTHORIZE + "?oauth_token=" + event.token.key;
}
private function onLocationChange(event : Event) : void
{
var location : String = htmlWindow.location;
var callbackIndex : int = location.search(Twitter.TWITTER_CALLBACK_URL);
if ((callbackIndex == 0) && (location.search("oauth_token") > -1))
{
twitter.addEventListener(TwitterOAuthEvent.ACCESS_TOKEN, accessTokenHandler);
twitter.obtainAccessToken();
}
}
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 verticalCenter="0" horizontalCenter="0"
includeIn="unauthenticated">
<mx1:HTML id="htmlWindow"/>
</s:Group>
<s:Group verticalCenter="0" horizontalCenter="0"
includeIn="authenticated">
<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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment