You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
注册

Laravel Socialite调用LinkedIn API无法获取特定字段求助

Hey there! Let's fix that issue with accessing LinkedIn's extended fields (like publicProfileUrl, positions, and summary) from Laravel Socialite's response. Here's what's going on and how to solve it:

The Problem

Socialite's Laravel\Socialite\Two\User object stores the raw LinkedIn API response in a protected $user property—this means you can't directly access it with $linkedin_user->user or array-style indexing like $linkedin_user['user']. Your previous attempts failed because of this protected access restriction.

The Solution

Use Socialite's built-in method to retrieve the full raw API response, then access the fields you need normally:

  1. Get the raw user data
    Socialite provides a raw() method (or getRaw() in older versions) that returns the complete array of data from LinkedIn's API:

    $rawLinkedInData = $linkedin_user->raw();
    
  2. Access your target fields
    Now you can pull out publicProfileUrl, summary, positions, etc., using standard array access (with null coalescing to handle missing data gracefully):

    $publicProfileUrl = $rawLinkedInData['publicProfileUrl'] ?? null;
    $summary = $rawLinkedInData['summary'] ?? null;
    // For positions, grab the values array (handle empty cases)
    $positions = $rawLinkedInData['positions']['values'] ?? [];
    
  3. Save to your database
    Update your user creation code to include these fields. If you're storing structured data like positions, use json_encode() to save it as a JSON column in your database:

    $user = User::where('provider_id', $linkedin_user->getId())->first();
    
    if (!$user) {
        $user = new User;
        $user->name = $linkedin_user->getName();
        $user->email = $linkedin_user->getEmail();
        $user->picture = $linkedin_user->getAvatar();
        $user->provider_id = $linkedin_user->getId();
        $user->access_token = $linkedin_user->token;
        
        // Add the LinkedIn-specific fields
        $user->linkedin_profile_url = $publicProfileUrl;
        $user->linkedin_summary = $summary;
        $user->linkedin_positions = json_encode($positions);
        
        $user->save();
    }
    

Quick Check: Permissions

One last thing—make sure your LinkedIn app has the correct API permissions to retrieve these fields! For example:

  • r_liteprofile gives access to basic profile data (name, avatar)
  • To get summary, positions, and publicProfileUrl, you may need additional permissions like r_fullprofile (verify LinkedIn's current API docs for exact requirements, as permissions are occasionally updated).

If you don't have the right permissions, LinkedIn won't return those fields in the response—so double-check your app's settings on the LinkedIn Developer Portal.

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

火山引擎 最新活动