Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. public async Task<string> Get(string email, string username, string password)
  2. {
  3. GlobalVariables.InstagramLogins.Remove(email);
  4. if (GlobalVariables.InstagramLogins.TryGetValue(email, out var api))
  5. {
  6. return Newtonsoft.Json.JsonConvert.SerializeObject(new { status = "already_logged_in" });
  7. }
  8. GlobalVariables.InstagramLogins.Remove(email);
  9. var appData = AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "\\";
  10. string stateFile = appData + "user_states/" + InstagramExtension.CleanFromBadCharacters(email) + "_state.bin";
  11. // TODO Get instagram login from available accounts
  12. var userSession = new UserSessionData()
  13. {
  14. UserName = username,
  15. Password = password
  16. };
  17.  
  18. // TODO GET RANDOM UNUSED PROXY
  19. var instaApi = InstaApiBuilder.CreateBuilder()
  20. .SetUser(userSession)
  21. .UseLogger(new DebugLogger(LogLevel.All))
  22. //.SetRequestDelay(RequestDelay.FromSeconds(0, 1))
  23. // Session handler, set a file path to save/load your state/session data
  24. .SetSessionHandler(new FileSessionHandler() { FilePath = stateFile })
  25. .Build();
  26.  
  27. // Load previous sessios if it exists
  28. instaApi?.SessionHandler?.Load();
  29. if (instaApi != null && instaApi.IsUserAuthenticated)
  30. {
  31. // session load logged us in
  32. GlobalVariables.InstagramLogins.Add(email, instaApi);
  33. return Newtonsoft.Json.JsonConvert.SerializeObject(new { status = "logged_in" });
  34. // return instaApi;
  35. }
  36.  
  37. if (instaApi != null && !instaApi.IsUserAuthenticated)
  38. {
  39. var logInResult = await instaApi.LoginAsync();
  40. Debug.WriteLine(logInResult.Value);
  41.  
  42. if (logInResult.Succeeded)
  43. {
  44. // We logged in
  45. // lets save the session
  46. instaApi.SessionHandler.Save();
  47.  
  48. GlobalVariables.InstagramLogins.Add(email, instaApi);
  49. return Newtonsoft.Json.JsonConvert.SerializeObject(new { status = "logged_in" });
  50. //return instaApi;
  51. }
  52.  
  53. if (logInResult.Value == InstaLoginResult.TwoFactorRequired)
  54. {
  55. return Newtonsoft.Json.JsonConvert.SerializeObject(new { status = "2fa_required" });
  56. }
  57. // Could not log in, lets see why
  58. if (logInResult.Value == InstaLoginResult.ChallengeRequired)
  59. {
  60. GlobalVariables.InstagramLogins.Add(email, instaApi);
  61. return Newtonsoft.Json.JsonConvert.SerializeObject(new { status = logInResult.Info.Message, loginresult = Newtonsoft.Json.JsonConvert.SerializeObject(logInResult) });
  62. }
  63.  
  64. else
  65. {
  66. return Newtonsoft.Json.JsonConvert.SerializeObject(new { status = logInResult.Info.Message });
  67. }
  68. }
  69.  
  70. return Newtonsoft.Json.JsonConvert.SerializeObject(new { status = "could_not_log_in" });
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement