You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于已有C# Instagram类实现帖子评论与点赞功能

Implementing Instagram Post Likes and Comments in C# (Extending Your Existing Class)

Alright, let's walk through how to add like and comment functionality to your existing Instagram class in C#. First, a quick heads-up: Instagram's web API is pretty finicky and changes often, so you’ll want to stick to their terms of service and keep an eye out for endpoint shifts down the line. Also, make sure your HttpManager is properly persisting cookies from the login session—this is critical for authenticating subsequent requests.

First: Get the Post's Media ID

Most of Instagram's web endpoints use the post's internal media ID (not the shortcode you see in the URL, like ABC123 in https://www.instagram.com/p/ABC123/). Let's add a helper method to fetch that ID from a shortcode:

// You'll need a JSON parser like Newtonsoft.Json or System.Text.Json installed
public string GetMediaIdFromShortcode(string shortcode)
{
    // The __a=1 parameter forces Instagram to return JSON instead of HTML
    string response = http.getResponse($"https://www.instagram.com/p/{shortcode}/?__a=1");
    
    // Parse the JSON to extract the media ID (example using Newtonsoft.Json)
    dynamic postData = Newtonsoft.Json.JsonConvert.DeserializeObject(response);
    return postData.graphql.shortcode_media.id;
}

Adding the Like Functionality

Once you have the media ID, you can send a POST request to Instagram's like endpoint. Here's the method:

public bool LikePost(string mediaId)
{
    // Grab the latest CSRF token from your stored cookies (your HttpManager should expose this)
    string csrfToken = http.GetCookieValue("csrftoken");
    
    var postValues = new System.Collections.Specialized.NameValueCollection();
    postValues.Add("csrfmiddlewaretoken", csrfToken);
    
    // The web endpoint for liking a post
    string likeEndpoint = $"https://www.instagram.com/web/likes/{mediaId}/like/";
    
    // Make sure your PostResponse method sends cookies from the login session and sets headers like X-Requested-With: XMLHttpRequest
    string response = http.PostResponse(likeEndpoint, postValues);
    
    // Check if the request succeeded (Instagram returns a JSON with "status": "ok" on success)
    dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(response);
    return result.status == "ok";
}

Adding the Comment Functionality

Commenting works similarly—use the media ID and send your comment text to the comments endpoint:

public bool AddComment(string mediaId, string commentText)
{
    string csrfToken = http.GetCookieValue("csrftoken");
    
    var postValues = new System.Collections.Specialized.NameValueCollection();
    postValues.Add("csrfmiddlewaretoken", csrfToken);
    postValues.Add("comment", commentText);
    postValues.Add("replied_to_comment_id", ""); // Leave empty for top-level comments
    
    string commentEndpoint = $"https://www.instagram.com/web/comments/{mediaId}/add/";
    string response = http.PostResponse(commentEndpoint, postValues);
    
    dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(response);
    return result.status == "ok";
}

Key Things to Keep in Mind

  • Headers Matter: Your HttpManager needs to send headers that mimic a real browser, like a valid User-Agent, Referer (set to the post's URL), and X-Requested-With: XMLHttpRequest—Instagram blocks requests that don't look legitimate.
  • Rate Limits: Instagram will throttle or block you if you send too many requests too quickly. Add delays between actions and handle 429 (Too Many Requests) status codes.
  • Session Expiry: Login sessions don't last forever. Add logic to check if your requests are failing due to an expired session, and re-run the login method when needed.
  • Terms of Service: Automating likes/comments might violate Instagram's terms. Use this only for personal, non-spammy use.

内容的提问来源于stack exchange,提问作者Sid Brooklyn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:57:44