Created
April 3, 2012 16:05
-
-
Save vadimii/2293206 to your computer and use it in GitHub Desktop.
How to get user data from vk.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instructions: http://goo.gl/2JIC2 | |
static void Main(string[] args) | |
{ | |
var apiUrl = "http://api.vk.com/api.php"; | |
var dict = new Dictionary<string, string>(); | |
dict["api_id"] = "123456"; // Change this! | |
dict["v"] = "3.0"; | |
dict["format"] = "JSON"; | |
var api_secret = "1JH6TCDi8VBGyxQAZQ1Q"; // Change this! | |
dict["method"] = "getProfiles"; | |
dict["uids"] = "4780160"; | |
dict["fields"] = "photo,nickname,screen_name,birthdate"; | |
var keys = from k in dict.Keys orderby k select k; | |
var signStringItems = from k in keys select string.Format("{0}={1}", k, dict[k]); | |
var signString = string.Join(string.Empty, signStringItems.ToArray()) + api_secret; | |
var md5Hasher = MD5.Create(); | |
var md5Data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(signString)); | |
var md5Chars = from b in md5Data select b.ToString("x2"); | |
var md5String = string.Join(string.Empty, md5Chars.ToArray()); | |
dict["sig"] = md5String; | |
Func<KeyValuePair<string, string>, string> formatParamValue = kv => | |
string.Format("{0}={1}", kv.Key, Uri.EscapeDataString(kv.Value)); | |
var queryItems = from kv in dict select formatParamValue(kv); | |
var queryString = string.Join("&", queryItems.ToArray()); | |
var requestUrl = string.Format("{0}?{1}", apiUrl, queryString); | |
var responseJson = (string)null; | |
using (var browser = new WebClient()) | |
using (var responseData = browser.OpenRead(requestUrl)) | |
using (var responseReader = new StreamReader(responseData)) | |
{ | |
responseJson = responseReader.ReadToEnd(); | |
} | |
var jsonUtil = new JavaScriptSerializer(); | |
var user = jsonUtil.Deserialize<VKResponse>(responseJson).response[0]; | |
} | |
class VKResponse | |
{ | |
public VKUserInfo[] response { get; set; } | |
} | |
class VKUserInfo | |
{ | |
public string uid { get; set; } | |
public string first_name { get; set; } | |
public string last_name { get; set; } | |
public string photo { get; set; } | |
public string birthday { get; set; } | |
public string nickname { get; set; } | |
public string screen_name { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment