forked from: Nakamap OAuth2.0 API Sample

by ohisama forked from Nakamap OAuth2.0 API Sample (diff: 56)
♥0 | Line 197 | Modified 2013-03-15 11:13:05 | MIT License
play

ActionScript3 source code

/**
 * Copyright ohisama ( http://wonderfl.net/user/ohisama )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/kuHy
 */

// forked from 9re's Nakamap OAuth2.0 API Sample
package 
{
    import flash.display.Loader;
    import flash.events.IOErrorEvent;
    import flash.errors.IOError;
    import flash.system.Security;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.text.TextFormatAlign;
    import flash.text.TextFormat;
    import flash.net.URLVariables;
    import flash.net.URLRequestMethod;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;
    import flash.events.MouseEvent;
    import flash.text.TextFieldType;
    import flash.text.TextField;
    import flash.display.Sprite;
    import com.adobe.serialization.json.JSON;    
    public class FlashTest extends Sprite 
    {
        private static const CLIENT_ID : String = "client_id";
        private static const CLIENT_SECRET : String = "client_secret";
        private static const AUTH_DIALOG_ENDPOINT : String = "https://nakamap.com/dialog/oauth";
        private static const ACCESS_TOKEN_ENDPOINT : String = "https://thanks.nakamap.com/oauth/access_token";
        private static const READ_BASIC : String = "read_basic";
        private static const WRITE_BASIC : String = "write_basic";
        private var _forms : Object = {};        
        public function FlashTest() 
        {
            stage.align = "tl";
            stage.scaleMode = "noScale";
            var auth_code : String = loaderInfo.parameters.code;
            showHeader(); 
            createForm(CLIENT_ID, 10, 50);
            createForm(CLIENT_SECRET, 10, 85);
            Security.loadPolicyFile("https://thanks.nakamap.com/crossdomain.xml");
            if (auth_code) 
            {
                getAccessToken(auth_code);
            } 
            else 
            {
                showStartAuth();
            }
        }        
        private function getAccessToken(auth_code : String) : void 
        {
            var log : TextField = new TextField;
            log.y = 150;
            log.width = 465;
            log.appendText('got auth code: ' + auth_code + '\n\n');
            addChild(log);            
            createButton("get access token", 210, 120, function () : void 
            {
                var vars : URLVariables = new URLVariables;
                vars.grant_type = "authorization_code";
                vars.code = auth_code;
                vars.redirect_uri = getSWFUrl();
                vars[CLIENT_ID] = getValue(CLIENT_ID);
                vars[CLIENT_SECRET] = getValue(CLIENT_SECRET);
                var req : URLRequest = new URLRequest;
                req.url = ACCESS_TOKEN_ENDPOINT;
                req.data = vars;
                req.method = URLRequestMethod.POST;
                var ldr : URLLoader = new URLLoader;
                ldr.addEventListener(Event.COMPLETE, function () : void 
                {
                    var obj : Object = JSON.decode(ldr.data);
                    tryProtectedAPI(obj.access_token);
                });
                ldr.addEventListener(IOErrorEvent.IO_ERROR, function () : void 
                {
                    // auth error or any other
                });
                ldr.load(req);
            });
        }        
        private function tryProtectedAPI(token : String) : void 
        {
            var vars : URLVariables = new URLVariables;
            vars.token = token;
            const me : String = "https://thanks.nakamap.com/1/me";
            var req : URLRequest = new URLRequest;
            req.url = me;
            req.method = URLRequestMethod.GET;
            req.data = vars;            
            var ldr : URLLoader = new URLLoader;
            ldr.addEventListener(Event.COMPLETE, function () : void 
            {
                var obj : Object = JSON.decode(ldr.data);
                renderProfile(obj);
            });
            ldr.addEventListener(IOErrorEvent.IO_ERROR, function () : void 
            {
                // auth error or any other
            });
            ldr.load(req);
        }        
        private function renderProfile(prof : Object) : void 
        {
            var sp : Sprite = new Sprite;
            var ldr : Loader = new Loader;
            var tfProf : TextField = new TextField;
            tfProf.text = prof.name + '\n' + prof.description;
            sp.addChild(tfProf);
            ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, function () : void 
            {
                tfProf.x = ldr.width + 10;
            });
            ldr.load(new URLRequest(prof.icon));
            sp.addChild(ldr);
            sp.y = 200;
            sp.x = 10;
            addChild(sp);
        }
        private function showHeader() : void 
        {
            var tf : TextField = new TextField;
            tf.background = true;
            tf.backgroundColor = 0x333333;
            tf.textColor = 0xffffff;
            tf.mouseEnabled = false;
            tf.width = 465;
            tf.height = 20;
            var tfm : TextFormat = new TextFormat;
            tfm.align = TextFormatAlign.RIGHT;
            tf.text = "nakamap api oauth sample";
            tf.setTextFormat(tfm);
            addChild(tf);
        }        
        private function showStartAuth() : void 
        {
            createButton("start auth", 230, 120, function () : void 
            {
                var vars : URLVariables = new URLVariables;
                vars[CLIENT_ID] = getValue(CLIENT_ID);
                vars.response_type = "code";
                vars.scope = ([READ_BASIC, WRITE_BASIC]).join(" ");
                vars.redirect_uri = getSWFUrl();
                var req : URLRequest = new URLRequest;
                req.url = AUTH_DIALOG_ENDPOINT;
                req.method = URLRequestMethod.GET;
                req.data = vars;
                navigateToURL(req, "_blank");
            });
        }        
        private function getSWFUrl() : String 
        {
            var url : String = loaderInfo.loaderURL;
            var query_start : int = url.indexOf("?");
            return query_start > -1 ? url.substring(0, query_start) : url;
        }        
        private function getValue($name : String) : String 
        {
            var input : TextField = _forms[$name] as TextField;
            return input.text;
        }        
        private function createButton($label : String, $x : int, $y : int, $onClick : Function) : void 
        {
            var sp : Sprite = new Sprite;
            sp.buttonMode = true;
            sp.tabEnabled = false;
            var tf : TextField = new TextField;
            tf.border = true;
            tf.mouseEnabled = false;
            tf.text = $label;
            tf.width = tf.textWidth + 4; 
            tf.height = tf.textHeight + 4;
            sp.addEventListener(MouseEvent.CLICK, $onClick);
            sp.addChild(tf);
            sp.x = $x; 
            sp.y = $y;
            addChild(sp);
        }
        private function createForm($label : String, $x : int, $y : int) : void 
        {
            const LABEL_WIDTH : int = 60;
            var sp : Sprite = new Sprite;
            sp.x = $x;
            sp.y = $y;
            var lb : TextField = new TextField;
            lb.text = $label + ":";
            lb.x = LABEL_WIDTH - lb.textWidth + 2;
            lb.mouseEnabled = false;
            sp.addChild(lb);
            var input : TextField = new TextField;
            input.type = TextFieldType.INPUT;
            input.displayAsPassword = true;
            input.width = 200;
            input.height = 20;
            input.border = true;
            input.x = $x + LABEL_WIDTH;
            _forms[$label] = input;
            sp.addChild(input);
            addChild(sp);
        }
    }
}