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

如何通过单次API请求获取LinkedIn的姓名、邮箱及头像?

Hey there! Great question—let's break this down clearly so you can get all that user data in one go.

Can you retrieve a LinkedIn user's first name, last name, email, and profile avatar in a single API request?

Absolutely! While LinkedIn's standard v2 API uses separate endpoints for basic profile data and email, you can leverage their Batch API to combine both calls into a single HTTP request. This way, you send one request and receive all the required data in a single response.

How to implement this

Here's a step-by-step, actionable guide to make this work:

1. Set up your LinkedIn Developer App

  • Head to the LinkedIn Developer Portal and create a new app. Once created, grab your unique Client ID and Client Secret.
  • Critical step: Ensure your app is approved for the required scopes:
    • r_liteprofile: Required to access the user's name and profile picture
    • r_emailaddress: Required to retrieve the user's email address
      (Note: If you're building a third-party tool, LinkedIn will review your app to ensure your use case aligns with their policies before granting these scopes.)

2. Handle OAuth 2.0 Authorization

LinkedIn uses OAuth 2.0 to let users grant your app access to their data. Follow this flow:

  • Redirect your user to LinkedIn's authorization URL with the required scopes:
    https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&scope=r_liteprofile%20r_emailaddress
    
  • After the user approves access, you'll receive an authorization code. Exchange this code for an access token by sending a POST request to LinkedIn's token endpoint:
    POST https://www.linkedin.com/oauth/v2/accessToken
    
    Include these form data parameters: grant_type=authorization_code, code=YOUR_AUTH_CODE, client_id=YOUR_CLIENT_ID, client_secret=YOUR_CLIENT_SECRET, redirect_uri=YOUR_REDIRECT_URI

3. Make the Batch API Request

Now use the Batch API to fetch both profile and email data in one call. Send a POST request to https://api.linkedin.com/v2/batch with these headers:

  • Authorization: Bearer YOUR_ACCESS_TOKEN
  • X-Restli-Protocol-Version: 2.0.0
  • Content-Type: application/json

Here's the JSON body for your request:

{
  "requests": [
    {
      "id": "profile-call",
      "method": "GET",
      "url": "/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))"
    },
    {
      "id": "email-call",
      "method": "GET",
      "url": "/emailAddress?q=members&projection=(elements*(handle~))"
    }
  ]
}

4. Parse the Response

The response will include two entries matching the ids you specified. Here's how to extract your desired data:

  • First/Last Name: From the profile-call response, use firstName.localized.en_US and lastName.localized.en_US (adjust the locale code if you need names in a different language).
  • Profile Avatar: In the profile-call response, navigate to profilePicture.displayImage~.elements. This array contains multiple image variants—pick the one with the highest resolution (look for the identifier field, which is the direct image URL).
  • Email: From the email-call response, grab elements[0].handle~.emailAddress (this is the user's primary email address).

Quick Tips

  • Always handle errors: Check for invalid access tokens, missing scope permissions, or rate limits in the response.
  • Localization support: If your app serves global users, use the appropriate locale code (e.g., es_ES for Spanish) instead of en_US.
  • App compliance: Be transparent about why you need this data during LinkedIn's app review process to speed up approval.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 22:03:17