如何评论和喜欢使用c#的Instagram帖子

时间:2018-04-08 09:17:30

标签: c# .net instagram

我有一个像这样定义的Instagram类(我得到了这个代码,我需要实现一些新的东西):

class Instagram
    {
        public HttpManager http = new HttpManager();

        public bool login(string username, string password)
        {
            string source = http.getResponse("https://www.instagram.com/accounts/login/?force_classic_login");

            var csrfmiddlewaretoken = http.ParseBetween(source, "\"csrfmiddlewaretoken\" value=\"", "\"");

            var values = new NameValueCollection();
            values.Add("username", username);
            values.Add("password", password);
            values.Add("csrfmiddlewaretoken", csrfmiddlewaretoken);

            http.Referer = "https://www.instagram.com/accounts/login/?force_classic_login";
            source = http.getResponse("https://www.instagram.com/accounts/login/?force_classic_login", values);

            return source.Contains("\"" + username + "\"");
        }

        public List<KeyValuePair<string, string> > getLatestImages(string tag)
        {
            string source = http.getResponse("https://www.instagram.com/explore/tags/" + tag + "/");

            var json = http.ParseBetween(source, "window._sharedData = ", ";</script>");

            dynamic obj = JObject.Parse(json);
            obj = obj.entry_data.TagPage[0].graphql.hashtag.edge_hashtag_to_media.edges;

            var returnList = new List<KeyValuePair<string, string>>();

            foreach (var post in obj)
            {
                string imageurl = post.node.thumbnail_resources[1].src; // post.node.display_url;
                string posturl = "https://www.instagram.com/p/" + post.node.shortcode;
                returnList.Add(new KeyValuePair<string, string>(imageurl, posturl));
            }

            return returnList;
        }
    }

我得到一组具有特定主题标签的特定帖子。

var insta = new Instagram();
insta.login("myusername", "mypassword");

Task.Run(() =>
{
    var res = insta.getLatestImages("myhashtag"); 

    foreach (var s in res)
    {
        Console.WriteLine(s.Key + " WITH myhashtag");
    }
});

在我的问题(标题问题)之前,我需要说我读了Instagram API和包装器InstaSharp。这个解决方案(我需要在IAPI和IS之后实现新事物而不是从头开始)和示例(IAPI和IS)之间存在差异。例如......使用InstaSharp,很容易喜欢帖子(但我不能使用它):

/// <summary>
        /// Set a like on this media by the currently authenticated user.
        /// <para>Requires Authentication: False</para>
        /// <para>
        /// <c>Required Scope: </c>likes
        /// </para>
        /// </summary>
        /// <param name="mediaId">The id of the media to create a like for.</param>
        /// <returns>LikesResponse</returns>
        public Task<LikesResponse> Post(string mediaId)
        {
            AssertIsAuthenticated();

            var request = Request("{id}/likes", HttpMethod.Post);
            request.AddUrlSegment("id", mediaId);
            return Client.ExecuteAsync<LikesResponse>(request);
        }

我也在更新一个类文件(HttpManager)。任何答案或提示都非常受欢迎。最后,再一次,我需要根据现有代码制定解决方案。

2 个答案:

答案 0 :(得分:1)

无需自己实施这些选项。 instagram有很多开源库。

InstagramApiSharp是主题之一。

发送评论媒体:

var commentResponse = await InstaApi.CommentProcessor.CommentMediaAsync("MEDIA ID", "Hey Keep it up!");

像媒体:

var likeResponse = await InstaApi.MediaProcessor.LikeMediaAsync("MEDIA ID");

答案 1 :(得分:-1)

我使用了开源的InstaSharp。