Install-Package Newtonsoft.Json
private void Button_GetToken_Click(object sender, EventArgs e) { AuthorizationForm GetToken = new AuthorizationForm(); GetToken.ShowDialog(); }
using System; using System.Windows.Forms; using System.IO; namespace VkAPITutorial { public partial class AuthorizationForm : Form { public AuthorizationForm() { InitializeComponent(); } private void AuthorizationForm_Load(object sender, EventArgs e) { GetToken.DocumentCompleted += GetToken_DocumentCompleted; GetToken.Navigate("https://oauth.vk.com/authorize?client_id="+ VkAPI.__APPID +"&display=page&redirect_uri=https://oauth.vk.com/blank.html&scope=friends&response_type=token&v=5.52"); } private void GetToken_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (GetToken.Url.ToString().IndexOf("access_token=") != -1) { GetUserToken(); } } private void GetUserToken() { char[] Symbols = { '=', '&' }; string[] URL = GetToken.Url.ToString().Split(Symbols); File.WriteAllText("UserInf.txt", URL[1] + "\n"); File.AppendAllText("UserInf.txt", URL[5]); this.Visible = false; } private void GetToken_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e){} } }
https://oauth.vk.com/authorize?client_id=ApplicationID&display=page&redirect_uri=https://oauth.vk.com/blank.html&scope=friends&response_type=token&v=5.52
using System.Collections.Generic; using xNet; using Newtonsoft.Json; namespace VkAPITutorial { class VkAPI { public const string __APPID = "APPLICATION_ID"; //ID private const string __VKAPIURL = "https://api.vk.com/method/"; // private string _Token; //, public VkAPI(string AccessToken) { _Token = AccessToken; } public Dictionary<string, string> GetInformation(string UserId, string[] Fields) // ID { HttpRequest GetInformation = new HttpRequest(); GetInformation.AddUrlParam("user_ids", UserId); GetInformation.AddUrlParam("access_token", _Token); GetInformation.AddUrlParam("version", "5.52"); string Params = ""; foreach (string i in Fields) { Params += i + ","; } Params = Params.Remove(Params.Length - 1); GetInformation.AddUrlParam("fields", Params); string Result = GetInformation.Get(__VKAPIURL + "users.get").ToString(); Result = Result.Substring(13, Result.Length - 15); Dictionary<string, string> Dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(Result); return Dict; } public string GetCityById(string CityId) // ID { HttpRequest GetCityById = new HttpRequest(); GetCityById.AddUrlParam("city_ids", CityId); GetCityById.AddUrlParam("access_token", _Token); GetCityById.AddUrlParam("version", "5.52"); string Result = GetCityById.Get(__VKAPIURL + "database.getCitiesById").ToString(); Result = Result.Substring(13, Result.Length - 15); Dictionary<string, string> Dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(Result); return Dict["name"]; } public string GetCountryById(string CountryId) // ID { HttpRequest GetCountryById = new HttpRequest(); GetCountryById.AddUrlParam("country_ids", CountryId); GetCountryById.AddUrlParam("access_token", _Token); GetCountryById.AddUrlParam("version", "5.52"); string Result = GetCountryById.Get(__VKAPIURL + "database.getCountriesById").ToString(); Result = Result.Substring(13, Result.Length - 15); Dictionary<string, string> Dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(Result); return Dict["name"]; } } }
using System; using System.Windows.Forms; using System.Collections.Generic; using System.IO; namespace VkAPITutorial { public partial class MainForm : Form { VkAPI _ApiRequest; private string _Token; //, private string _UserId; //ID private Dictionary<string, string> _Response; // public MainForm() { InitializeComponent(); } private void Button_GetToken_Click_1(object sender, EventArgs e) { AuthorizationForm GetToken = new AuthorizationForm(); GetToken.ShowDialog(); } private void MainForm_Load(object sender, EventArgs e) { try { StreamReader ControlInf = new StreamReader("UserInf.txt"); _Token = ControlInf.ReadLine(); _UserId = ControlInf.ReadLine(); ControlInf.Close(); if (_Token != null) { _ApiRequest = new VkAPI(_Token); string[] Params = { "city", "country", "photo_max" }; _Response = _ApiRequest.GetInformation(_UserId, Params); if (_Response != null) { User_ID.Text = _UserId; User_Photo.ImageLocation = _Response["photo_max"]; User_Name.Text = _Response["first_name"]; User_Surname.Text = _Response["last_name"]; User_Country.Text = _ApiRequest.GetCountryById(_Response["country"]); User_City.Text = _ApiRequest.GetCityById(_Response["city"]); Button_GetToken.Visible = false; } } } catch { } } private void Button_GetInformation_Click_1(object sender, EventArgs e) { try { StreamReader ControlInf = new StreamReader("UserInf.txt"); _Token = ControlInf.ReadLine(); ControlInf.Close(); _ApiRequest = new VkAPI(_Token); _UserId = User_ID.Text; string[] Params = { "city", "country", "photo_max" }; _Response = _ApiRequest.GetInformation(_UserId, Params); if (_Response != null) { User_ID.Text = _UserId; User_Photo.ImageLocation = _Response["photo_max"]; User_Name.Text = _Response["first_name"]; User_Surname.Text = _Response["last_name"]; User_Country.Text = _ApiRequest.GetCountryById(_Response["country"]); User_City.Text = _ApiRequest.GetCityById(_Response["city"]); Button_GetToken.Visible = false; } } catch { } } } }
StreamReader ControlInf = new StreamReader("UserInf.txt"); _Token = ControlInf.ReadLine(); _UserId = ControlInf.ReadLine(); ControlInf.Close();
_ApiRequest = new VkAPI(_Token); string[] Params = { "city", "country", "photo_max" }; _Response = _ApiRequest.GetInformation(_UserId, Params);
User_ID.Text = _UserId; User_Photo.ImageLocation = _Response["photo_max"]; User_Name.Text = _Response["first_name"]; User_Surname.Text = _Response["last_name"]; User_Country.Text = _ApiRequest.GetCountryById(_Response["country"]); User_City.Text = _ApiRequest.GetCityById(_Response["city"]); Button_GetToken.Visible = false;
private void Button_GetInformation_Click(object sender, EventArgs e) { try { StreamReader ControlInf = new StreamReader("UserInf.txt"); _Token = ControlInf.ReadLine(); ControlInf.Close(); _ApiRequest = new VkAPI(_Token); _UserId = User_ID.Text; string[] Params = { "city", "country", "photo_max" }; _Response = _ApiRequest.GetInformation(_UserId, Params); if (_Response != null) { User_ID.Text = _UserId; User_Photo.ImageLocation = _Response["photo_max"]; User_Name.Text = _Response["first_name"]; User_Surname.Text = _Response["last_name"]; User_Country.Text = _ApiRequest.GetCountryById(_Response["country"]); User_City.Text = _ApiRequest.GetCityById(_Response["city"]); Button_GetToken.Visible = false; } } catch { } }
HttpRequest GetInformation = new HttpRequest();
GetInformation.AddUrlParam("user_ids", UserId); GetInformation.AddUrlParam("access_token", _Token); GetInformation.AddUrlParam("version", "5.52"); string Params = ""; foreach (string i in Fields) { Params += i + ","; } Params = Params.Remove(Params.Length - 1); GetInformation.AddUrlParam("fields", Params);
string Result = GetInformation.Get(__VKAPIURL + "users.get").ToString();
Result = Result.Substring(13, Result.Length - 15);
Dictionary<string, string> Dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(Result);
Source: https://habr.com/ru/post/314518/
All Articles