<?xml version= "1.0" encoding= "utf-8" ?>
<mx:Application xmlns:mx= "http://www.adobe.com/2006/mxml"
layout= "absolute"
width= "607" height= "590"
applicationComplete= "onAppStart()" >
<mx:Style>
Text,Label
{
color: "0xFFFFFF" ;
font-size: 24;
}
</mx:Style>
<mx:states>
<mx:State name= "enable_friends" >
<mx:RemoveChild target= "{mainContainer}" />
<mx:AddChild position= "lastChild" >
<mx:Text
text= " . '' ' '. ."
selectable= "false"
horizontalCenter= "0" verticalCenter= "0"
width= "570" height= "200" />
</mx:AddChild>
</mx:State>
</mx:states>
<mx:VBox
id= "mainContainer"
horizontalCenter= "0" verticalCenter= "0"
width= "590"
maxHeight= "570"
horizontalAlign= "center" >
<mx:Label
id= "userNameLabel"
text= ", %username%" />
<mx:Label id= "friendsCaptionLabel" text= " :" />
</mx:VBox>
<mx:Script>
<![CDATA[
import vkontakte.VKontakteAPI;
private function onAppStart() : void
{
// pass flashvars to the vKontakte init
VKontakteAPI.init( this .parameters);
// request user info for current user
VKontakteAPI.getProfiles([VKontakteAPI.uid], onGetUserInfo);
// request friends
VKontakteAPI.getFriends(onGetFriends);
}
private function onGetUserInfo(result : Object) : void
{
// set user label text to the user first_name + last_name
userNameLabel.text =
", " +
result.response[0].first_name +
" " +
result.response[0].last_name;
}
private function onGetFriends(result : Object) : void
{
if (result.error)
{
// if app have not access to friends
if (result.error.error_code == 7)
{
// tell user to allow access
currentState = "enable_friends" ;
return ;
}
return ;
}
// no error
// have not friends
if (result.response.length <= 0)
{
var no_friends_label : Label = new Label();
no_friends_label.text = " (" ;
mainContainer.addChild(no_friends_label);
return ;
}
// have friends, get info
VKontakteAPI.getProfiles(result.response, onGetFriendsInfo);
}
private function onGetFriendsInfo(result : Object) : void
{
for ( var i : int = 0; i < result.response.length; i++)
{
var friend_label : Label = new Label();
friend_label.text = result.response[i].first_name + " " + result.response[i].last_name;
mainContainer.addChild(friend_label);
}
}
]]>
</mx:Script>
</mx:Application>
* This source code was highlighted with Source Code Highlighter .
public class VKontakteAPI
{
// application secret key
private static const APP_SECRET : String = "SuxPmMMxDj" ;
// stored application id to use when running locally
private static var API_ID : String = "1643226" ;
// stored user id to use when running locally
private static var UID : String = "52531344" ;
// test mode for running in sandbox mode
// replace by "0" after switching application to public access
private static var TEST_MODE : String = "1" ;
* This source code was highlighted with Source Code Highlighter .
// return array of values included in all requests
private static function getBaseValues() : Array
{
return [
"api_id=" + API_ID,
"v=2.0" ,
"format=json" ,
"test_mode=" + TEST_MODE
];
}
* This source code was highlighted with Source Code Highlighter .
// returns MD5 as required by vKontakte API
private static function getMD5(values : Array) : String
{
// sort values alphabetically
values.sort();
var hash_str : String = "" ;
hash_str += UID;
for ( var i : int = 0; i < values.length; i++)
hash_str += values[i];
hash_str += APP_SECRET;
return MD5.hex_md5(hash_str);
}
* This source code was highlighted with Source Code Highlighter .
// combine request string
private static function getRequestString(values : Array) : String
{
var request : String = "http://api.vkontakte.ru/api.php" ;
for ( var i : int = 0; i < values.length; i++)
request += (i == 0 ? "?" : "&" ) + values[i];
return request;
}
* This source code was highlighted with Source Code Highlighter .
// main request function
// method - vKontakteAPI method name (like "getUserInfo" or "getProfiles")
// add_values - addition method parameters (ex. for "getProfiles"
// add_values must contain list of uids like "uids=123,3124,3123")
// callback - function called after completing request
private static function makeRequest(method : String, add_values : String, callback : Function) : void
{
// base values for all requests
var values : Array = getBaseValues();
// add method name
values.push( "method=" + method);
// add additional values if have any
if (add_values)
values.push(add_values);
// calculate md5 hash and add it to values array
values.push( "sig=" + getMD5(values));
// request loader
var loader : URLLoader = new URLLoader();
// register listener for COMPLETE event
loader.addEventListener(
Event.COMPLETE,
function ( event : Event) : void
{
// extract loader from event
var loader : URLLoader = URLLoader( event .target);
// parse json data and pass it
// to callback function
callback(JParser.decode(loader.data));
});
// fire request with url created from values
loader.load( new URLRequest(getRequestString(values)));
}
* This source code was highlighted with Source Code Highlighter .
// must be called at application start
// to init API variables (or left default values when running locally)
public static function init(flashvars : Object) : void
{
// if have viewer_id in flashvars
if (flashvars.viewer_id)
{
// then it means that application started in vKontakte framework
// update userID for user whos started application
UID = flashvars.viewer_id;
}
}
* This source code was highlighted with Source Code Highlighter .
// get basic user(s) data (uid, first_name, last_name) for provided uids array
public static function getProfiles(uids : Array, callback : Function) : void
{
var uids_str : String = "uids=" + uids[0];
for ( var i : int = 1; i < uids.length; i++)
uids_str += "," + uids[i];
makeRequest( "getProfiles" , uids_str, callback);
}
// returns friends of the current user
public static function getFriends(callback : Function) : void
{
makeRequest( "getFriends" , null , callback);
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/71814/
All Articles