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

使用LinkedIn Voyager API遇问题,求排查Cookie使用及代码错误

LinkedIn Voyager API 请求问题排查建议

核心问题排查点

  • Cookie格式错误:你的cookies变量包含换行符,JavaScript中未转义的换行会破坏字符串结构,导致Cookie头格式无效。标准Cookie格式要求键值对用; (分号加空格)分隔,需将Cookie字符串合并为一行。
  • 请求方法不匹配:/identity/profiles/端点通常用于GET请求获取用户/公司列表,POST方法并非该端点的合法请求方式,会被服务端拒绝。
  • Content-Type设置错误:multipart/form-data用于文件上传场景,而Voyager API的请求大多使用application/json或application/x-www-form-urlencoded,错误的Content-Type会导致服务端无法解析请求。
  • 请求头冲突与缺失:手动设置Cookie后,credentials: "include"会触发浏览器自动附加Cookie,造成冲突;同时Voyager API要求User-Agent、X-RestLi-Protocol-Version等必填头,缺失会被拦截。

修正后的示例代码

const companyLink = 'https://www.linkedin.com/voyager/api/identity/profiles/';

// 修正Cookie格式:移除换行,用标准分隔符
const cookies = "cookie1=nj9ucrk32lohmd89mbuab5mjra79t61o8c3u8bgl; cookie2=ae0f5d277f1255f7cf869fcf2bf914; cookie3=ae0f5d277f1255f7cf869fcf2bf914";

async function fetchDetails(){
    try{
        const response = await fetch(companyLink,{
            method:'GET', // 匹配端点的合法请求方法
            headers:{
                'Cookie': cookies,
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
                'X-Li-Lang': 'en_US',
                'X-RestLi-Protocol-Version': '2.0.0' // Voyager API必填协议头
            }
        });
        
        // 先验证响应状态
        if (!response.ok) {
            throw new Error(`请求失败: ${response.status} ${response.statusText}`);
        }
        
        const data = await response.json(); // Voyager API返回JSON格式数据
        console.log(data);
        return data;
    }
    catch(error){
        console.error('请求错误:', error);
    }
}
fetchDetails();

额外排查建议

  1. 对照浏览器真实请求:登录LinkedIn后,通过开发者工具Network面板查看Voyager API的请求头、Cookie、方法,确保代码与真实请求一致。
  2. 确认Cookie有效性:检查Cookie是否过期,尤其是li_at(LinkedIn核心认证Cookie)是否存在且有效。
  3. 规避跨域限制:浏览器环境下直接请求会触发CORS拦截,建议使用Node.js环境或代理服务发起请求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 07:02:13