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

基于jQuery/Node实现LinkedIn邀请发送遇错求助

解决LinkedIn邀请发送的两个常见错误

一、前端JS SDK报错:Uncaught (in promise) TypeError: Cannot read property 'credentials' of null

这个错误核心原因是用户未完成有效授权或者使用了已废弃的API版本/格式,可以按以下步骤修复:

  1. 检查LinkedIn应用配置

    • 登录LinkedIn开发者平台,找到你的应用,确保Valid OAuth Redirect URIs配置了当前页面的完整域名(比如http://localhost:8080,必须和页面地址完全匹配)。
    • 确认应用已经申请了发送邀请所需的权限:w_member_social(写社交内容权限)、r_liteprofile(读取基础个人资料)、r_emailaddress(读取邮箱)。
  2. 更新SDK初始化与代码逻辑

    • LinkedIn v1 API已废弃,必须使用v2版本,初始化时指定api_version;
    • 先检查用户是否授权,未授权则引导授权;
    • 调整请求端点和请求体格式,适配v2 API。

修正后的代码示例:

<!DOCTYPE html>
<html>
<head>
<script>
function invite() {
  // 先检查用户是否已授权
  if (!IN.User.isAuthorized()) {
    IN.User.authorize();
    return;
  }

  // v2 API的邀请请求体格式
  const body = {
    "invitee": {
      "emailAddress": "dineshsharma.developer@gmail.com",
      "firstName": "Dinesh",
      "lastName": "Sharma"
    },
    "message": "Say yes! Let's connect on LinkedIn."
  };

  IN.API.Raw()
    .url('/rest/invitationRequests') // v2的正确端点
    .method("POST")
    .header('Content-Type', 'application/json')
    .body(JSON.stringify(body))
    .result(function (result) {
      console.log("邀请发送成功:", result);
    })
    .error(function (error) {
      console.log("发送失败:", error);
    });
}
</script>
<script type="text/javascript" src="https://platform.linkedin.com/in.js">
  api_key: '8145jr5s78ow'
  onLoad: invite
  authorize: true
  api_version: 'v2' // 指定v2版本
</script>
</head>
<body>
</body>
</html>

二、Node.js linkedin-auto-connect包报错:Request failed with status code 303

303状态码表示重定向请求,这个问题大概率是因为该包依赖的API端点已变更,或者OAuth配置不正确:

  1. 排查配置问题

    • 确认你的LinkedIn应用的client ID、client secret、redirect URI和包的配置完全一致,尤其是redirect URI必须和开发者平台中配置的完全匹配。
    • 检查包的文档,确认它是否支持LinkedIn v2 API,如果是基于v1开发的,必然会出现兼容性问题。
  2. 推荐使用官方SDK替代
    第三方包可能维护不及时,建议直接使用LinkedIn官方的Node.js SDK @linkedin/api-client,示例代码如下:

首先安装依赖:

npm install @linkedin/api-client

然后编写发送邀请的代码:

const { Client } = require('@linkedin/api-client');

const client = new Client({
  clientId: 'YOUR_CLIENT_ID',
  clientSecret: 'YOUR_CLIENT_SECRET',
  redirectUri: 'YOUR_REDIRECT_URI'
});

// 注意:需要先通过OAuth2流程获取accessToken,这里假设你已经拿到了有效token
client.setAccessToken('YOUR_ACCESS_TOKEN');

async function sendInvitation() {
  try {
    const response = await client.invitationRequests.create({
      invitee: {
        emailAddress: 'dineshsharma.developer@gmail.com',
        firstName: 'Dinesh',
        lastName: 'Sharma'
      },
      message: 'Say yes! Let\'s connect.'
    });
    console.log('邀请发送成功:', response.data);
  } catch (error) {
    console.error('发送失败:', error.response.data);
  }
}

sendInvitation();

额外注意事项

  • 所有LinkedIn API调用都需要用户的有效授权,确保accessToken没有过期;
  • 发送邀请的频率不要过高,避免触发LinkedIn的反垃圾机制。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 09:28:18