I am working on integrating the social network “vkontakte” into my desktop application using Adobe AIR. The flow of authorization in the app is as follows:
-
Open the web browser with a url, and when the user inputs login and password, redirect to
somePage.html. -
From that URL, I need to get an
access_tokento sign all future API requests.
So the question really is: How can I listen to an event, which will indicate that the user has logged in successfully, or failed to get that access_token?
Here is the code I am using:
package
{
import air.net.URLMonitor;
import flash.display.Sprite;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.StatusEvent;
import flash.net.*;
public class VKPlayer extends Sprite
{
private var url:URLRequest;
public function VKPlayer()
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
//draw black background
with( graphics )
{
beginFill(0x0)
drawRect(0, 0, stage.stageWidth, stage.stageHeight);
}
init();
}
private function init():void
{
trace("Starting application");
//vk init params
var scope:String = "friends,video,audio";
var appID:String = "1111111";
var redirect_uri:String = "http://oauth.vkontakte.ru/blank.html";
var browserType:String = "popup";
var response_type:String = "token";
var baseURL:String = "http://oauth.vkontakte.ru/authorize?client_id=";
var initUrl:String = baseURL + appID + "&scope=" + scope + "&redirect_uri="+redirect_uri+"&display="+browserType+"&response_type="+response_type;
trace(initUrl);
url = new URLRequest(initUrl);
navigateToURL(url, "_blank");
makeQueries();
}
private function makeQueries():void
{
var str:String = "https://api.vkontakte.ru/method/getProfiles?uid=36396528&access_token=23ef280b0482a7604aa9191a414bef70b421bd54bc475e1cc7fa3d8a854d8ac";
var req:URLRequest = new URLRequest(str);
var uLdr:URLLoader = new URLLoader(req);
uLdr.addEventListener(Event.COMPLETE, onComplete);
uLdr.addEventListener(ErrorEvent.ERROR, onErrorHandler);
uLdr.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function onComplete(event:Event):void
{
trace(event);
}
private function onErrorHandler(event:ErrorEvent):void
{
trace(event);
}
private function ioErrorHandler(event:IOErrorEvent):void
{
trace(event);
}
}
}
Your answer is tagged with Flash so I’m not sure if this is applicable as I think it may only be available in the Flex SDK.
The Flex SDK contains a
HTMLcomponent which lets you display HTML content in your application.This will essentially create a holder for a webpage – in this case your authorisation page. Once the user has authorised you app, you can listen for a
LOCATION_CHANGEevent which will occur when the application redirects you to a sucess or failure page.With some services, they will pass the Access token in the url that they redirect you to, so you can inspect the new location and extract the access token from there.