0

Flash SDK updated to v1.1

Yesterday at MAX we rolled out an update to the Flash SDK which addresses some issues with using OAuth and should hopefully make the process of obtaining a session easier for certain applications.

Included in the update, a new AuthenticationRequest class was added that allows you to get a request token, send a user to the Allow Access page, and then get an access token. This access token is what you need to make authenticated requests into the Social Platform to obtain any sort of data about your user.

Download the SDK:
http://developer.yahoo.com/flash/yos/ (Version 1.1)

Here is some code:

CODE
   var _session:YahooSession;
var _user:YahooUser;

var _token:OAuthToken;

// create a session by passing the consumer key and secret only.
_session = new YahooSession("ABC123", "456XYZ");

// grab the sessions auth object.
var _auth:AuthenticationRequest = _session.auth;

// get a request token and listen for the success event.
_auth.addEventListener(YahooResultEvent.GET_REQUEST_TOKEN_SUCCESS, handleRequestTokenSuccess);
_auth.getRequestToken();

function handleRequestTokenSuccess(event:YahooResultEvent):void
{
// save the request token and then use it to send the user to the authorize page
// then after the user has finished, we'll use it again in order to request an access token.

_token = event.data as OAuthToken;
_auth.sendToAuthorization(_token);
}

// The userAuthFinish method should be called after the user has indicated
// that they have finished the authorization process. (ie: on button click)

function userAuthFinish():void
{
// reuse the request token to request an access token.

_auth.addEventListener(YahooResultEvent.GET_ACCESS_TOKEN_SUCCESS, handleAccessTokenSuccess);
_auth.getAccessToken(_token);
}

function handleAccessTokenSuccess(event:YahooResultEvent):void
{
// save the access token and create a new session.
_token = event.data as OAuthToken;

// set the sessions token.
_session.setAccessToken(_token);

getUserProfile();
}

function getUserProfile():void
{
_user = _session.getSessionedUser();
_user.profile.addEventListener(YahooResultEvent.GET_PROFILE_SUCCESS, handleGetProfileSuccess);
_user.profile.getProfile();
}

function handleGetProfileSuccess(event:YahooResultEvent):void
{
var profile:Profile = event.data as Profile;
// do something
}


This update should be most useful for developers wishing to build AIR applications using the Social SDKs, as this gives you the ability to run through the entire OAuth flow in a desktop app with just a few method calls and callback functions. Also, using the file system access in AIR, you can easily store the access token on the client in a file. (Or use the new encrypted SQLite database in AIR 1.5)

CODE
function readAndSetToken():void
{
// read the token
var token:OAuthToken = readToken();

// try to the set the token, if its expired we'll use it to request a new one from Yahoo.
if(_session.setAccessToken(token))
{
// ready to go.
}
else
{
_auth.addEventListener(YahooResultEvent.GET_ACCESS_TOKEN_SUCCESS, handleAccessTokenSuccess);
_auth.getAccessToken(token);
}
}

function handleAccessTokenSuccess(event:YahooResultEvent):void
{
var token:OAuthToken = event.data as OAuthToken;
trace("new token expires at:", new Date(token.tokenExpires).toString());

if(_session.setAccessToken(token))
{
writeToken();
// ready to go.
}
}

// writes the current session token to the file system as an AMF object.
private function writeToken():void
{
var fs:FileStream = new FileStream();
fs.open(File.applicationStorageDirectory.resolvePath("AuthAccess.txt"), FileMode.WRITE);
fs.writeObject( _session.getAccessToken() );
fs.close();
}

// reads the token data if the file exists
private function readToken():OAuthToken
{
var f:File = File.applicationStorageDirectory.resolvePath("AuthAccess.txt");

if(f.exists)
{
var fs:FileStream = new FileStream();
fs.open(f, FileMode.READ);

// read the file data object and parse it back into an OAuthToken
var token:OAuthToken = AuthenticationRequest.toToken(fs.readObject());
fs.close();

return token;
}
else return null;
}


One last note, if you are building a YAP application, the container will pass the access token and access token secret in the POST variables to your server, therefore, you do not need to go through this flow. Alternatively, if you prefer authentication to happen on the server, the PHP SDK is the easiest way to make that happen.

Hope this helps

-Zach Graves

by
7 Replies
  • May I know when we can download the Flash SDK 1.1? I still can only download 1.0 from the download link.
    I really look forward to 1.1 version.

    Before the version 1.1, may I know how I can do to obtain to OAuth token and OAuth access token?


    Thanks!
    0
  • QUOTE (stanleyfok0403 @ Nov 30 2008, 07:54 AM) <{POST_SNAPBACK}>
    May I know when we can download the Flash SDK 1.1? I still can only download 1.0 from the download link.
    I really look forward to 1.1 version.

    Before the version 1.1, may I know how I can do to obtain to OAuth token and OAuth access token?


    Thanks!


    Whoops. Something happened with the docs... we'll get them back up asap.
    0
  • QUOTE (Zach graves @ Dec 1 2008, 02:11 PM) <{POST_SNAPBACK}>
    Whoops. Something happened with the docs... we'll get them back up asap.


    Fixed. Let me know if there are any further issues.

    Prior to 1.1, the method for getting an access token / secret was by passing them into your application via flashvars from the POST data.
    0
  • Thanks so much.

    I have justed tested the SDK version 1.1 with Flash CS3 on my Desktop. It's working fine!

    However, every time I open the Flash, it prompts me to the verification page in the broswer, which makes the development time longer and affects the user experience. Any way to change that?

    Thanks again!
    0
  • QUOTE (stanleyfok0403 @ Dec 2 2008, 07:59 AM) <{POST_SNAPBACK}>
    Thanks so much.

    I have justed tested the SDK version 1.1 with Flash CS3 on my Desktop. It's working fine!

    However, every time I open the Flash, it prompts me to the verification page in the broswer, which makes the development time longer and affects the user experience. Any way to change that?

    Thanks again!


    Ooosh! I just read your code about AIR on saving and reading the token. I think that can solve my problem. Thanks!
    0
  • QUOTE (stanleyfok0403 @ Dec 2 2008, 08:08 AM) <{POST_SNAPBACK}>
    Ooosh! I just read your code about AIR on saving and reading the token. I think that can solve my problem. Thanks!


    Right on. That is the expected flow for a user if you haven't saved the access token object somewhere (shared object, local file, etc.) and will re-use it.
    0
  • Hi, I've downloaded the Flash SDK.
    I found there is no produce update API in Flash SDK but in PHP SDK.
    Do you have a plan to do that?
    Thanks~
    0

Recent Posts

in YAP