LAST.FM API AUTH

A large part of my application is the use of the last.fm API to generate customised music listening to replace the radio generated playlist. I have been making good use of actionscript 3 last.fm api over on google code. It has been a huge help in getting last.fm data in to Flash. However, it seems to be an early version, as certain methods are far from complete, including the auth method used to authenticate users.

I have rewritten parts of the auth method so that it is called correctly. Particularly the current requestToken() parameter was a little clunky as it involved copying and pasting a token from a url back into the lastFMBase class once the user had logged in. I have amended the function as follows, and added an userLogin() function which directs them to the URL.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public function requestToken():void
{
  var variablesTest:Array = new Array();
  variablesTest.push(new KeyValue("api_key", LastFMBase.API_KEY));
  variablesTest.push(new KeyValue("method", "auth.getToken"));
  var url:String = "?method=auth.getToken&api_key=" + LastFMBase.API_KEY + "&api_sig=" + createAPI_Signature(variablesTest);
  requestURL(url, URLRequestMethod.GET);
  loader.addEventListener(Event.COMPLETE, function (event:Event):void {dispatchEvent(new Event(Auth.TOKEN_LOADED))});
}
 
public function userLogin():void
{
  var request:URLRequest = new URLRequest("http://www.last.fm/api/auth/?" + "api_key=" + LastFMBase.API_KEY + "&token=" + TOKEN);
  navigateToURL(request);
}

Using this format, auth.requestToken() is called first, and an event listener waits for the TOKEN_LOADED event. Once this has happened the user can be directed to their browser to login to last.fm using auth.userLogin. Once signed in the user is prompted to click a button, as my application is a desktop one it helps simplify things a bit. This calls the auth.getSession event.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public function getToken()
{
  _auth.requestToken();
  _auth.addEventListener(Auth.TOKEN_LOADED, tokenLoaded);
}
 
public function tokenLoaded(event:Event):void {
  trace("token trace: " + _auth.xml.token);
  LastFMBase.TOKEN = _auth.xml.token;
  _auth.userLogin();
}
 
public function getSession(event:MouseEvent) 
{	
  _auth.getSession();
  _auth.addEventListener(Auth.SESSION_LOADED, getSessionHandler);
}

Tags: , , , , ,

One Response to “LAST.FM API AUTH”

  1. Hu Says:

    Hello.
    How getSession is called ?
    Thx

Leave a Reply