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

Instagram公开API访问受限:Salesforce APEX请求解决方案咨询

问题:Instagram无登录访问APEX请求被限制的解决方案

问题背景

  • 原本无需登录即可访问Instagram帖子原始内容,早期使用?__a=1参数即可获取,该方式失效后改用?__a=1&__d=dis参数
  • 该参数在浏览器中能正常获取帖子/Reel内容,但在AWS环境中请求会被重定向至登录页
  • 通过Salesforce APEX实现时,首次请求可成功拿到响应:

    {"graphql":{"shortcode_media":....

  • 但从第二次请求开始,即使等待数分钟,每次都会收到限流响应:

    {"message":"Please wait a few minutes before you try again.","require_login":true,"status":"fail"}

当前使用的APEX代码

public class InstagramRequest {
    public static HttpResponse sendRequest() {
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://www.instagram.com/p/CtHVCv9vdf4/?__a=1&__d=dis');
        request.setMethod('GET');
        request.setHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36');
        Http http = new Http();
        HttpResponse response = http.send(request);
        return response;
    }
}

解决方案及代码修改建议

1. 复用会话Cookie

Instagram通过Cookie识别会话,首次请求成功后需保存Cookie,后续请求带上以维持会话状态:

  • 添加静态变量存储Cookie值
  • 首次请求后从响应头提取Set-Cookie并保存
  • 后续请求设置Cookie头

2. 完善请求头,模拟真实浏览器

补充常见浏览器请求头,降低被识别为爬虫的概率:

  • 添加Accept、Accept-Language、Referer、Accept-Encoding等头信息
  • 可轮换不同的User-Agent,避免固定值

3. 控制请求频率,添加随机延迟

加入随机延迟避免频繁触发限流机制,注意APEX的执行时间限制

修改后的示例代码

public class InstagramRequest {
    // 静态变量存储会话Cookie
    private static String sessionCookie;
    // 可选的User-Agent列表,随机选择
    private static List<String> userAgents = new List<String>{
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15',
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0'
    };

    public static HttpResponse sendRequest() {
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://www.instagram.com/p/CtHVCv9vdf4/?__a=1&__d=dis');
        request.setMethod('GET');
        request.setFollowRedirects(false); // 关闭自动重定向,手动处理

        // 随机选择User-Agent
        Integer randomIndex = Math.mod(Math.round(Math.random() * 100), userAgents.size());
        request.setHeader('User-Agent', userAgents[randomIndex]);

        // 添加模拟浏览器的请求头
        request.setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8');
        request.setHeader('Accept-Language', 'en-US,en;q=0.5');
        request.setHeader('Referer', 'https://www.instagram.com/');
        request.setHeader('Accept-Encoding', 'gzip, deflate, br');
        request.setHeader('DNT', '1');
        request.setHeader('Connection', 'keep-alive');
        request.setHeader('Upgrade-Insecure-Requests', '1');

        // 如果存在会话Cookie,添加到请求头
        if (String.isNotBlank(sessionCookie)) {
            request.setHeader('Cookie', sessionCookie);
        }

        Http http = new Http();
        HttpResponse response = http.send(request);

        // 首次请求成功后保存Cookie
        if (response.getStatusCode() == 200 && String.isBlank(sessionCookie)) {
            List<String> setCookies = response.getHeaderValues('Set-Cookie');
            if (setCookies != null && !setCookies.isEmpty()) {
                // 提取Cookie值,拼接成请求可用的格式
                List<String> cookieParts = new List<String>();
                for (String cookie : setCookies) {
                    // 截取到分号前的部分,去掉属性
                    Integer semicolonIndex = cookie.indexOf(';');
                    if (semicolonIndex > 0) {
                        cookieParts.add(cookie.substring(0, semicolonIndex));
                    } else {
                        cookieParts.add(cookie);
                    }
                }
                sessionCookie = String.join(cookieParts, '; ');
            }
        }

        // 模拟随机延迟,避免频繁请求(注意APEX最大执行时间限制)
        try {
            Integer delaySeconds = Math.round(Math.random() * 20) + 10; // 10-30秒随机延迟
            Thread.sleep(delaySeconds * 1000);
        } catch (Exception e) {
            // 忽略延迟异常
        }

        return response;
    }
}

额外注意事项

  • 如果仍然触发限流,可尝试更换请求的IP地址(Salesforce APEX的出口IP是固定的,可能需要使用代理服务)
  • Instagram的API参数和反爬机制可能随时更新,需定期验证参数有效性

内容的提问来源于stack exchange,提问作者Charan Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 16:20:43