Exemplo n.º 1
0
        /// <summary>
        ///     Create new API instance
        /// </summary>
        /// <returns>
        ///     API instance
        /// </returns>
        /// <exception cref="ArgumentNullException">User auth data must be specified</exception>
        public IInstaApi Build()
        {
            if (_user == null)
            {
                throw new ArgumentNullException("User auth data must be specified");
            }
            if (_httpClient == null)
            {
                _httpClient = new HttpClient(_httpHandler)
                {
                    BaseAddress = new Uri(InstaApiConstants.INSTAGRAM_URL)
                }
            }
            ;

            if (_requestMessage == null)
            {
                if (_device == null)
                {
                    _device = AndroidDeviceGenerator.GetRandomAndroidDevice();
                }
                _requestMessage = new ApiRequestMessage
                {
                    PhoneId  = _device.PhoneGuid.ToString(),
                    Guid     = _device.DeviceGuid,
                    Password = _user?.Password,
                    Username = _user?.UserName,
                    DeviceId = ApiRequestMessage.GenerateDeviceId(),
                    AdId     = _device.AdId.ToString()
                };
            }

            if (string.IsNullOrEmpty(_requestMessage.Password))
            {
                _requestMessage.Password = _user?.Password;
            }
            if (string.IsNullOrEmpty(_requestMessage.Username))
            {
                _requestMessage.Username = _user?.UserName;
            }

            if (_device == null && !string.IsNullOrEmpty(_requestMessage.DeviceId))
            {
                _device = AndroidDeviceGenerator.GetById(_requestMessage.DeviceId);
            }
            if (_device == null)
            {
                AndroidDeviceGenerator.GetRandomAndroidDevice();
            }

            if (_httpRequestProcessor == null)
            {
                _httpRequestProcessor =
                    new HttpRequestProcessor(_delay, _httpClient, _httpHandler, _requestMessage, _logger);
            }

            var instaApi = new InstaApi(_user, _logger, _device, _httpRequestProcessor);

            return(instaApi);
        }
Exemplo n.º 2
0
        public IInstaApi Build()
        {
            if (_httpClient == null)
            {
                _httpClient             = new HttpClient(_httpHandler);
                _httpClient.BaseAddress = new Uri(InstaApiConstants.INSTAGRAM_URL);
            }
            AndroidDevice device = null;

            if (_requestMessage == null)
            {
                device          = AndroidDeviceGenerator.GetRandomAndroidDevice();
                _requestMessage = new ApiRequestMessage
                {
                    phone_id  = device.PhoneGuid.ToString(),
                    guid      = device.DeviceGuid,
                    password  = _user?.Password,
                    username  = _user?.UserName,
                    device_id = ApiRequestMessage.GenerateDeviceId()
                };
            }
            var instaApi = new InstaApi(_user, _logger, _httpClient, _httpHandler, _requestMessage, device);

            return(instaApi);
        }
Exemplo n.º 3
0
        public static string GenerateFacebookUserAgent()
        {
            var deviceInfo = AndroidDeviceGenerator.GetRandomAndroidDevice();

            //Mozilla/5.0 (Linux; Android 7.0; PRA-LA1 Build/HONORPRA-LA1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36

            return(string.Format(InstaApiConstants.FACEBOOK_USER_AGENT,
                                 deviceInfo.AndroidVer.VersionNumber, deviceInfo.DeviceModelIdentifier,
                                 $"{deviceInfo.AndroidBoardName}{deviceInfo.DeviceModel}"));
        }
Exemplo n.º 4
0
        public static IInstaApi GetDefaultInstaApiInstance(UserSessionData user)
        {
            var device         = AndroidDeviceGenerator.GetByName(AndroidDevices.SAMSUNG_NOTE3);
            var requestMessage = ApiRequestMessage.FromDevice(device);
            var apiInstance    = InstaApiBuilder.CreateBuilder()
                                 .SetUser(user)
                                 .SetApiRequestMessage(requestMessage)
                                 .SetRequestDelay(TimeSpan.FromSeconds(2))
                                 .Build();

            return(apiInstance);
        }
Exemplo n.º 5
0
        public static IInstaApi GetDefaultInstaApiInstance(string username)
        {
            var device         = AndroidDeviceGenerator.GetByName(AndroidDevices.SAMSUNG_NOTE3);
            var requestMessage = ApiRequestMessage.FromDevice(device);
            var apiInstance    = InstaApiBuilder.CreateBuilder()
                                 .SetUserName(username)
                                 .UseLogger(new DebugLogger(LogLevel.All))
                                 .SetApiRequestMessage(requestMessage)
                                 .Build();

            return(apiInstance);
        }
 /// <summary>
 ///     Set custom android device.
 ///     <para>Note: this is optional, if you didn't set this, InstagramApiSharp will choose random device.</para>
 /// </summary>
 /// <param name="androidDevice">Android device</param>
 /// <returns>API Builder</returns>
 public IInstaApiBuilder SetDevice(AndroidDevice androidDevice)
 {
     if (androidDevice == null)
     {
         _device = AndroidDeviceGenerator.GetRandomAndroidDevice();
     }
     else
     {
         _device = androidDevice;
     }
     return(this);
 }
Exemplo n.º 7
0
        public static IInstaApi GetDefaultInstaApiInstance(UserSessionData user)
        {
            var device         = AndroidDeviceGenerator.GetByName(AndroidDevices.SAMSUNG_NOTE3);
            var requestMessage = ApiRequestMessage.FromDevice(device);
            var apiInstance    = new InstaApiBuilder()
                                 .SetUser(user)
                                 .UseLogger(new TestLogger())
                                 .SetApiRequestMessage(requestMessage)
                                 .Build();

            return(apiInstance);
        }
Exemplo n.º 8
0
        public IInstaApi Build()
        {
            if (_httpClient == null)
            {
                _httpClient = new HttpClient(_httpHandler)
                {
                    BaseAddress = new Uri(InstaApiConstants.INSTAGRAM_URL)
                }
            }
            ;

            if (_requestMessage == null)
            {
                _device         = AndroidDeviceGenerator.GetRandomAndroidDevice();
                _requestMessage = new ApiRequestMessage
                {
                    phone_id  = _device.PhoneGuid.ToString(),
                    guid      = _device.DeviceGuid,
                    password  = _user?.Password,
                    username  = _user?.UserName,
                    device_id = ApiRequestMessage.GenerateDeviceId()
                };
            }


            if (string.IsNullOrEmpty(_requestMessage.password))
            {
                _requestMessage.password = _user?.Password;
            }
            if (string.IsNullOrEmpty(_requestMessage.username))
            {
                _requestMessage.username = _user?.UserName;
            }
            if (_device == null && !string.IsNullOrEmpty(_requestMessage.device_id))
            {
                _device = AndroidDeviceGenerator.GetById(_requestMessage.device_id);
            }
            if (_device == null)
            {
                AndroidDeviceGenerator.GetRandomAndroidDevice();
            }

            if (_httpRequestProcessor == null)
            {
                _httpRequestProcessor =
                    new HttpRequestProcessor(_delay, _httpClient, _httpHandler, _requestMessage, _logger);
            }

            var instaApi = new InstaApi(_user, _logger, _device, _httpRequestProcessor);

            return(instaApi);
        }
Exemplo n.º 9
0
        private void GenerateDevice()
        {
            AndroidDevice device = AndroidDeviceGenerator.GetRandomAndroidDevice();

            string user = "", password = "";

            user     = usernameText.Text;
            password = passwordText.Text;

            ApiRequestMessage _requestMessage = new ApiRequestMessage
            {
                phone_id  = device.PhoneGuid.ToString(),
                guid      = device.DeviceGuid,
                password  = password,
                username  = user,
                device_id = AndroidDeviceGenerator.HTC10
            };

            HttpClient client = new System.Net.Http.HttpClient();

            client.BaseAddress = new Uri("https://i.instagram.com");

            UserSessionData userConfig = new UserSessionData();

            userConfig.UserName = user;
            userConfig.Password = password;

            HttpClientHandler httpClientHandler = new HttpClientHandler();


            /*Add this if you want to add proxy support.*/

            /*if (proxyFound)
             * {
             *  httpClientHandler.UseProxy = true;
             * IWebProxy proxy = new WebProxy("proxy");
             *  httpClientHandler.Proxy = proxy;
             * }
             */

            api = new InstaApi(userConfig, null, client, httpClientHandler, _requestMessage, device);

            InstagramLogin();
        }
Exemplo n.º 10
0
        /// <summary>
        ///     Create new API instance
        /// </summary>
        /// <returns>
        ///     API instance
        /// </returns>
        /// <exception cref="ArgumentNullException">User auth data must be specified</exception>
        public IInstaApi Build()
        {
            if (_user == null)
            {
                _user = UserSessionData.Empty;
            }

            if (_httpHandler == null)
            {
                _httpHandler = new HttpClientHandler();
            }

            if (_httpClient == null)
            {
                _httpClient = new HttpClient(_httpHandler)
                {
                    BaseAddress = new Uri(InstaApiConstants.INSTAGRAM_URL)
                }
            }
            ;

            if (_requestMessage == null)
            {
                if (_device == null)
                {
                    _device = AndroidDeviceGenerator.GetRandomAndroidDevice();
                }
                _requestMessage = new ApiRequestMessage
                {
                    PhoneId  = _device.PhoneGuid.ToString(),
                    Guid     = _device.DeviceGuid,
                    Password = _user?.Password,
                    Username = _user?.UserName,
                    DeviceId = ApiRequestMessage.GenerateDeviceId(),
                    AdId     = _device.AdId.ToString()
                };
            }

            if (string.IsNullOrEmpty(_requestMessage.Password))
            {
                _requestMessage.Password = _user?.Password;
            }
            if (string.IsNullOrEmpty(_requestMessage.Username))
            {
                _requestMessage.Username = _user?.UserName;
            }

            try
            {
                InstaApiConstants.TIMEZONE_OFFSET = int.Parse(DateTimeOffset.Now.Offset.TotalSeconds.ToString());
            }
            catch { }

            if (_httpRequestProcessor == null)
            {
                _httpRequestProcessor =
                    new HttpRequestProcessor(_delay, _httpClient, _httpHandler, _requestMessage, _logger);
            }

            if (_apiVersionType == null)
            {
                _apiVersionType = InstaApiVersionType.Version76;
            }

            var instaApi = new InstaApi(_user, _logger, _device, _httpRequestProcessor, _apiVersionType.Value);

            if (_sessionHandler != null)
            {
                _sessionHandler.InstaApi = instaApi;
                instaApi.SessionHandler  = _sessionHandler;
            }
            return(instaApi);
        }
Exemplo n.º 11
0
        public static async Task <bool> MainAsync()
        {
            try
            {
                Console.WriteLine("Starting demo of InstaSharper project");
                // create user session data and provide login details
                var userSession = new UserSessionData
                {
                    UserName = "******",
                    Password = "******"
                };

                // create new InstaApi instance using Builder
                var device         = AndroidDeviceGenerator.GetByName(AndroidDevices.SAMSUNG_NOTE3);
                var requestMessage = ApiRequestMessage.FromDevice(device);
                _instaApi = InstaApiBuilder.CreateBuilder()
                            .SetUser(userSession)
                            .SetApiRequestMessage(requestMessage)
                            .UseLogger(new DebugLogger(LogLevel.Info)) // use logger for requests and debug messages
                            .SetRequestDelay(TimeSpan.FromSeconds(2))
                            .Build();
                // login
                Console.WriteLine($"Logging in as {userSession.UserName}");
                var logInResult = await _instaApi.LoginAsync();

                if (!logInResult.Succeeded)
                {
                    Console.WriteLine($"Unable to login: {logInResult.Info.Message}");
                }
                else
                {
                    Console.WriteLine("Press 1 to start basic demo samples");
                    Console.WriteLine("Press 2 to start upload photo demo sample");
                    Console.WriteLine("Press 3 to start comment media demo sample");
                    Console.WriteLine("Press 4 to start stories demo sample");
                    Console.WriteLine("Press 5 to start demo with saving state of API instance");
                    Console.WriteLine("Press 6 to start messaging demo sample");

                    var samplesMap = new Dictionary <ConsoleKey, IDemoSample>
                    {
                        [ConsoleKey.D1] = new Basics(_instaApi),
                        [ConsoleKey.D2] = new UploadPhoto(_instaApi),
                        [ConsoleKey.D3] = new CommentMedia(_instaApi),
                        [ConsoleKey.D4] = new Stories(_instaApi),
                        [ConsoleKey.D5] = new SaveLoadState(_instaApi),
                        [ConsoleKey.D6] = new Messaging(_instaApi)
                    };
                    var key = Console.ReadKey();
                    Console.WriteLine(Environment.NewLine);
                    if (samplesMap.ContainsKey(key.Key))
                    {
                        await samplesMap[key.Key].DoShow();
                    }
                    Console.WriteLine("Done. Press esc key to exit...");

                    key = Console.ReadKey();
                    return(key.Key == ConsoleKey.Escape);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                var logoutResult = Task.Run(() => _instaApi.LogoutAsync()).GetAwaiter().GetResult();
                if (logoutResult.Succeeded)
                {
                    Console.WriteLine("Logout succeed");
                }
            }
            return(false);
        }
Exemplo n.º 12
0
        /// <summary>
        ///     Create new API instance
        /// </summary>
        /// <returns>
        ///     API instance
        /// </returns>
        /// <exception cref="ArgumentNullException">User auth data must be specified</exception>
        public IInstaApi Build()
        {
            if (user == null)
            {
                user = UserSessionData.Empty;
            }

            if (httpHandler == null)
            {
                httpHandler = new HttpClientHandler();
            }

            if (httpClient == null)
            {
                httpClient = new HttpClient(httpHandler)
                {
                    BaseAddress = new Uri(InstaApiConstants.InstagramUrl)
                };
            }

            if (requestMessage == null)
            {
                if (device == null)
                {
                    device = AndroidDeviceGenerator.GetRandomAndroidDevice();
                }

                requestMessage = new ApiRequestMessage
                {
                    PhoneId  = device.PhoneGuid.ToString(),
                    Guid     = device.DeviceGuid,
                    Password = user?.Password,
                    Username = user?.UserName,
                    DeviceId = ApiRequestMessage.GenerateDeviceId(),
                    AdId     = device.AdId.ToString()
                };
            }

            if (string.IsNullOrEmpty(requestMessage.Password))
            {
                requestMessage.Password = user?.Password;
            }

            if (string.IsNullOrEmpty(requestMessage.Username))
            {
                requestMessage.Username = user?.UserName;
            }

            try
            {
                InstaApiConstants.TimezoneOffset = (int)DateTimeOffset.Now.Offset.TotalSeconds;
            }
            catch
            {
            }

            if (httpRequestProcessor == null)
            {
                httpRequestProcessor = new HttpRequestProcessor(delay, httpClient, httpHandler, requestMessage, loggerFactory?.CreateLogger <HttpRequestProcessor>());
            }

            if (apiVersionType == null)
            {
                apiVersionType = InstaApiVersionType.Version86;
            }

            var instaApi = new InstaApi(loggerFactory?.CreateLogger <InstaApi>(),
                                        httpRequestProcessor,
                                        user,
                                        apiVersionType.Value,
                                        device,
                                        new CommonResilience(loggerFactory?.CreateLogger <CommonResilience>(),
                                                             ResilienceConfig.GenerateCommon()));

            return(instaApi);
        }
Exemplo n.º 13
0
        /// <summary>
        ///     Create new API instance
        /// </summary>
        /// <returns>
        ///     API instance
        /// </returns>
        /// <exception cref="ArgumentNullException">User auth data must be specified</exception>
        public IInstaApi Build()
        {
            if (_user == null)
            {
                throw new ArgumentNullException("User auth data must be specified");
            }

            if (_httpClient == null)
            {
                _httpClient = new HttpClient(_httpHandler)
                {
                    BaseAddress = new Uri(InstaApiConstants.INSTAGRAM_URL)
                }
            }
            ;

            if (_device == null)
            {
                _device = AndroidDeviceGenerator.GetRandomGoodDevice();
            }

            if (_requestMessage == null)
            {
                _requestMessage = new ApiRequestMessage
                {
                    device_id      = ApiRequestMessage.GenerateDeviceId(),
                    phone_id       = Guid.NewGuid().ToString(),
                    uuid           = Guid.NewGuid().ToString(), // "1365863f-bb91-4e9b-9c32-d3d6e5fda31f",
                    advertising_id = Guid.NewGuid().ToString(),
                    session_id     = Guid.NewGuid().ToString()
                };
            }

            if (string.IsNullOrEmpty(_requestMessage.password))
            {
                _requestMessage.password = _user?.Password;
            }

            if (string.IsNullOrEmpty(_requestMessage.username))
            {
                _requestMessage.username = _user?.UserName;
            }

            if (_cookieContainer != null)
            {
                _httpHandler.CookieContainer = _cookieContainer;
            }

            if (_cookies != null)
            {
                var str = "";
                foreach (var cookie in _cookies)
                {
                    str += (str == "" ? "" : ",") + cookie.Value;
                }
                _httpHandler.CookieContainer.SetCookies(new Uri("https://i.instagram.com"), str);
            }

            if (_httpRequestProcessor == null)
            {
                _httpRequestProcessor = new HttpRequestProcessor(_delay, _httpClient, _httpHandler, _requestMessage, _logger);
            }

            var instaApi = new InstaApi(_user, _logger, _device, _httpRequestProcessor);

            return(instaApi);
        }