1 module main; 2 3 import std.stdio; 4 import std.experimental.logger; 5 6 import vksdk; 7 8 void main() 9 { 10 TransportClient client = MarsTransportClient.getInstance; 11 VkApiClient vk = new VkApiClient(client); 12 VkKeys vkKeys = new VkKeys; 13 14 try { 15 AuthResponse authResponse = vk.oauth() 16 .userAuthorizationCodeFlow(vkKeys.appId, vkKeys.clientSecret, null, null) 17 .execute(); 18 19 writeln("access_token: ", authResponse.accessToken); 20 writeln("user_id: ", authResponse.userId); 21 writeln("expires_in: ", authResponse.expiresIn); 22 writeln("error: ", authResponse.error); 23 24 UserActor actor = new UserActor(authResponse.userId, authResponse.accessToken); 25 } catch (ApiException e1) { 26 log(e1.msg); 27 } catch (ClientException e2) { 28 log(e2.msg); 29 } 30 } 31 32 class VkKeys { 33 34 private immutable SETTINGS_FILE = "settings.ini"; 35 36 immutable int APP_ID = 123456; 37 immutable string CLIENT_SECRET = "CLIENT_SECRET"; 38 immutable string REDIRECT_URI = "REDIRECT_URI"; 39 immutable string CODE = "CODE"; 40 41 int appId; 42 string clientSecret; 43 string redirectUri; 44 string code; 45 46 this() { 47 import std.file : exists; 48 49 if (exists(SETTINGS_FILE)) { 50 import std.conv : to; 51 import dini; 52 53 auto ini = Ini.Parse(SETTINGS_FILE); 54 55 appId = to!int(ini["vk"].getKey("app_id")); 56 clientSecret = ini["vk"].getKey("client_secret"); 57 redirectUri = null; 58 code = null; 59 } else { 60 appId = APP_ID; 61 clientSecret = CLIENT_SECRET; 62 redirectUri = REDIRECT_URI; 63 code = CODE; 64 } 65 } 66 }