Ejemplo n.º 1
0
        public async Task <IResult <InstaUser> > GetUserAsync(string username)
        {
            ValidateUser();
            var userUri = UriCreator.GetUserUri(username);
            var request = HttpHelper.GetDefaultRequest(HttpMethod.Get, userUri, _deviceInfo);

            request.Properties.Add(new KeyValuePair <string, object>(InstaApiConstants.HEADER_TIMEZONE,
                                                                     InstaApiConstants.TIMEZONE_OFFSET.ToString()));
            request.Properties.Add(new KeyValuePair <string, object>(InstaApiConstants.HEADER_COUNT, "1"));
            request.Properties.Add(new KeyValuePair <string, object>(InstaApiConstants.HEADER_RANK_TOKEN, _user.RankToken));
            var response = await _httpClient.SendAsync(request);

            var json = await response.Content.ReadAsStringAsync();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var userInfo = JsonConvert.DeserializeObject <InstaSearchUserResponse>(json);
                var user     = userInfo.Users?.FirstOrDefault(u => u.UserName == username);
                if (user == null)
                {
                    string errorMessage = $"Can't find this user: {username}";
                    _logger.Write(errorMessage);
                    return(Result.Fail <InstaUser>(errorMessage));
                }
                var converter = ConvertersFabric.GetUserConverter(user);
                return(Result.Success(converter.Convert()));
            }
            return(Result.Fail(GetBadStatusFromJsonString(json).Message, (InstaUser)null));
        }
Ejemplo n.º 2
0
        public async Task <IResult <InstaUser> > GetCurrentUserAsync()
        {
            ValidateUser();
            ValidateLoggedIn();
            var     instaUri   = UriCreator.GetCurrentUserUri();
            dynamic jsonObject = new JObject();

            jsonObject._uuid      = _deviceInfo.DeviceGuid;
            jsonObject._uid       = _user.LoggedInUder.Pk;
            jsonObject._csrftoken = _user.CsrfToken;
            var fields = new Dictionary <string, string>
            {
                { "_uuid", _deviceInfo.DeviceGuid.ToString() },
                { "_uid", _user.LoggedInUder.Pk },
                { "_csrftoken", _user.CsrfToken }
            };
            var request = HttpHelper.GetDefaultRequest(HttpMethod.Post, instaUri, _deviceInfo);

            request.Content = new FormUrlEncodedContent(fields);
            var response = await _httpClient.SendAsync(request);

            var json = await response.Content.ReadAsStringAsync();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var user          = JsonConvert.DeserializeObject <InstaCurrentUserResponse>(json);
                var converter     = ConvertersFabric.GetUserConverter(user.User);
                var userConverted = converter.Convert();

                return(Result.Success(userConverted));
            }
            return(Result.Fail(GetBadStatusFromJsonString(json).Message, (InstaUser)null));
        }
Ejemplo n.º 3
0
        public async Task <InstaUser> GetUserAsync(string username)
        {
            string userUrl = $"{InstaApiConstants.INSTAGRAM_URL}{username}{InstaApiConstants.GET_ALL_POSTFIX}";
            IObjectConverter <InstaUser, InstaResponseUser> converter = null;
            var stream = await _httpClient.GetStreamAsync(userUrl);

            using (var reader = new StreamReader(stream))
            {
                var json          = reader.ReadToEnd();
                var root          = JObject.Parse(json);
                var userObject    = root["user"];
                var instaresponse = JsonConvert.DeserializeObject <InstaResponseUser>(userObject.ToString());
                converter = ConvertersFabric.GetUserConverter(instaresponse);
            }
            return(converter.Convert());
        }
Ejemplo n.º 4
0
        public async Task <IResult <bool> > LoginAsync()
        {
            ValidateUser();
            ValidateRequestMessage();
            try
            {
                var csrftoken     = string.Empty;
                var firstResponse = await _httpClient.GetAsync(_httpClient.BaseAddress);

                var cookies = _httpHandler.CookieContainer.GetCookies(_httpClient.BaseAddress);
                foreach (Cookie cookie in cookies)
                {
                    if (cookie.Name == InstaApiConstants.CSRFTOKEN)
                    {
                        csrftoken = cookie.Value;
                    }
                }
                _user.CsrfToken = csrftoken;
                var instaUri  = UriCreator.GetLoginUri();
                var signature = $"{_requestMessage.GenerateSignature()}.{_requestMessage.GetMessageString()}";
                var fields    = new Dictionary <string, string>
                {
                    { InstaApiConstants.HEADER_IG_SIGNATURE, signature },
                    { InstaApiConstants.HEADER_IG_SIGNATURE_KEY_VERSION, InstaApiConstants.IG_SIGNATURE_KEY_VERSION }
                };
                var request = HttpHelper.GetDefaultRequest(HttpMethod.Post, instaUri, _deviceInfo);
                request.Content = new FormUrlEncodedContent(fields);
                request.Properties.Add(InstaApiConstants.HEADER_IG_SIGNATURE, signature);
                request.Properties.Add(InstaApiConstants.HEADER_IG_SIGNATURE_KEY_VERSION,
                                       InstaApiConstants.IG_SIGNATURE_KEY_VERSION);
                var response = await _httpClient.SendAsync(request);

                var json = await response.Content.ReadAsStringAsync();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var loginInfo =
                        JsonConvert.DeserializeObject <InstaLoginResponse>(json);
                    IsUserAuthenticated = loginInfo.User != null && loginInfo.User.UserName == _user.UserName;
                    var converter = ConvertersFabric.GetUserConverter(loginInfo.User);
                    _user.LoggedInUder = converter.Convert();
                    _user.RankToken    = $"{_user.LoggedInUder.Pk}_{_requestMessage.phone_id}";
                    return(Result.Success(true));
                }
                else
                {
                    var loginInfo = GetBadStatusFromJsonString(json);
                    if (loginInfo.ErrorType == "checkpoint_logged_out")
                    {
                        return(Result.Fail("Please go to instagram.com and confirm checkpoint",
                                           ResponseType.CheckPointRequired, false));
                    }
                    if (loginInfo.ErrorType == "login_required")
                    {
                        return(Result.Fail("Please go to instagram.com and check if you account marked as unsafe",
                                           ResponseType.LoginRequired, false));
                    }
                    if (loginInfo.ErrorType == "Sorry, too many requests.Please try again later")
                    {
                        return(Result.Fail("Please try again later, maximum amount of requests reached",
                                           ResponseType.LoginRequired, false));
                    }
                    return(Result.Fail(loginInfo.Message, false));
                }
            }
            catch (Exception exception)
            {
                return(Result.Fail(exception.Message, false));
            }
        }