如何通过单次API请求获取LinkedIn的姓名、邮箱及头像?
Hey there! Great question—let's break this down clearly so you can get all that user data in one go.
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.
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 IDandClient Secret. - Critical step: Ensure your app is approved for the required scopes:
r_liteprofile: Required to access the user's name and profile picturer_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:
Include these form data parameters:POST https://www.linkedin.com/oauth/v2/accessTokengrant_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_TOKENX-Restli-Protocol-Version: 2.0.0Content-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-callresponse, usefirstName.localized.en_USandlastName.localized.en_US(adjust the locale code if you need names in a different language). - Profile Avatar: In the
profile-callresponse, navigate toprofilePicture.displayImage~.elements. This array contains multiple image variants—pick the one with the highest resolution (look for theidentifierfield, which is the direct image URL). - Email: From the
email-callresponse, grabelements[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_ESfor Spanish) instead ofen_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

