/// <summary>
        /// Get user profile => need logged in
        /// </summary>
        /// <returns></returns>
        private ProfileResult GetProfile()
        {
            Debug.WriteLine("Get profile");
            var request = HttpRequestBuilder.Get("https://www.instagram.com/accounts/edit/?__a=1", mCoockieC);

            request.Referer = $"https://www.instagram.com/{Username}/";
            request.Headers["X-Requested-With"] = "XMLHttpRequest";
            request.AllowAutoRedirect           = false;
            using (var response = request.GetResponse() as HttpWebResponse)
            {
                mCoockieC.Add(response.Cookies);
                using (var responseStream = response.GetResponseStream())
                    using (var gzipStream = new GZipStream(responseStream, CompressionMode.Decompress))
                        using (var streamReader = new StreamReader(gzipStream))
                        {
                            var data = streamReader.ReadToEnd();
                            return(JsonConvert.DeserializeObject <ProfileResult>(data));
                        }
            }
        }
 /// <summary>
 /// Retrieve user public info
 /// This method doesn't require logged in
 /// </summary>
 public PublicInfoResult.RootObject GetPublicInfo(string username)
 {
     try
     {
         var request = HttpRequestBuilder.Get($"https://www.instagram.com/{username}/?__a=1", mCoockieC);
         request.Referer = $"https://www.instagram.com/{username}/";
         request.Headers["X-Requested-With"] = "XMLHttpRequest";
         request.Timeout = 10000; // 10 seconds
         using (var response = request.GetResponse() as HttpWebResponse)
             using (var responseStream = response.GetResponseStream())
                 using (var gzipStream = new GZipStream(responseStream, CompressionMode.Decompress))
                     using (var streamReader = new StreamReader(gzipStream))
                     {
                         var data = streamReader.ReadToEnd();
                         return(JsonConvert.DeserializeObject <PublicInfoResult.RootObject>(data));
                     }
     }
     catch (Exception ex)
     {
         throw;
     }
 }
Example #3
0
        /// <summary>
        /// Like
        /// </summary>
        /// <param name="postId"></param>
        /// <returns></returns>
        public LikeResult Like(string postId)
        {
            // Note that to get postId, you simple get postlink then add /?__a=1
            // result return is jsonresult contain all infomation about this post.
            // you can like, comment,... example
            // https://www.instagram.com/p/BHtOw-7gKGs/?__a=1
            var request = HttpRequestBuilder.Post($"https://www.instagram.com/web/likes/{postId}/like/", mCoockieC);

            request.Referer = $"https://www.instagram.com";
            request.Headers["X-CSRFToken"]      = mCoockieC.GetCookies(new Uri("https://www.instagram.com"))["csrftoken"].Value;
            request.Headers["X-Instagram-AJAX"] = "1";
            request.Headers["X-Requested-With"] = "XMLHttpRequest";
            try
            {
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    mCoockieC.Add(response.Cookies);
                    if (request.HaveResponse)
                    {
                        using (var responseStream = response.GetResponseStream())
                            using (var streamReader = new StreamReader(responseStream))
                            {
                                var responseData = streamReader.ReadToEnd();
                                return(JsonConvert.DeserializeObject <Models.LikeResult>(responseData));
                            }
                    }
                    else
                    {
                        throw new WebException("no response");
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw ex;
            }
        }
Example #4
0
        /// <summary>
        /// Follow
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public FollowResult Follow(string username)
        {
            Debug.WriteLine("Following " + username);
            var publicInfo = GetPublicInfo(username);
            var request    = HttpRequestBuilder.Post($"https://www.instagram.com/web/friendships/{publicInfo.user.id}/follow/", mCoockieC);

            request.Referer = $"https://www.instagram.com/{publicInfo.user.username}/";
            request.Headers["X-CSRFToken"]      = mCoockieC.GetCookies(new Uri("https://www.instagram.com"))["csrftoken"].Value;
            request.Headers["X-Instagram-AJAX"] = "1";
            request.Headers["X-Requested-With"] = "XMLHttpRequest";
            try
            {
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    mCoockieC.Add(response.Cookies);
                    if (request.HaveResponse)
                    {
                        using (var responseStream = response.GetResponseStream())
                            using (var streamReader = new StreamReader(responseStream))
                            {
                                var responseData = streamReader.ReadToEnd();
                                return(JsonConvert.DeserializeObject <FollowResult>(responseData));
                            }
                    }
                    else
                    {
                        throw new WebException("no response");
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw ex;
            }
        }