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

使用LinkedIn OpenID Connect登录后无法获取用户邮箱、姓名等基础字段的技术咨询

LinkedIn OpenID Connect登录后无法获取用户邮箱、姓名等基础字段的技术咨询

我们正在开发一款名为SyncTerra的SaaS平台,采用LinkedIn的OpenID Connect实现用户登录。目前已经完成了以下配置:

  • 在LinkedIn开发者后台成功启用了“Sign in with LinkedIn using OpenID Connect”功能
  • 请求的权限范围包含:openid、profile、email
  • 配置了正确的redirect_uri和client_id

问题现象

用户完成登录跳转后,我们能成功获取到access token,但调用LinkedIn API时,返回的响应要么为空,要么不包含邮箱、名、姓这些基础字段。我们测试了多个LinkedIn账号,结果都是一样的,而且反复确认开发者后台的配置没有问题。

我们的实现代码

Flutter Web端登录触发代码

import 'dart:html' as html;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';

class LinkedInAuthDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        const String clientId = 'YOUR CLIENT ID';
        const String redirectUri = 'https://serwer2553879.home.pl/auth/linkedin/callback.php';
        const String state = 'syncTerraLogin123';
        const String scope = 'openid profile email';
        final String authUrl = 'https://www.linkedin.com/oauth/v2/authorization'
            '?response_type=code'
            '&client_id=$clientId'
            '&redirect_uri=$redirectUri'
            '&state=$state'
            '&scope=${Uri.encodeComponent(scope)}';
        html.window.location.href = authUrl;
      },
      child: Text('Login with LinkedIn'),
    );
  }
}

PHP回调处理代码(callback.php)

<?php
// Example LinkedIn callback handler (simplified)
if (isset($_GET['code'])) {
    $code = $_GET['code'];
    $clientId = 'YOUR CLIENT ID';
    $clientSecret = 'YOUR_CLIENT_SECRET';
    $redirectUri = 'https://serwer2553879.home.pl/auth/linkedin/callback.php';

    $tokenUrl = 'https://www.linkedin.com/oauth/v2/accessToken';
    $params = [
        'grant_type' => 'authorization_code',
        'code' => $code,
        'redirect_uri' => $redirectUri,
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $tokenUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($response, true);
    $accessToken = $data['access_token'] ?? null;

    if ($accessToken) {
        $profileUrl = 'https://api.linkedin.com/v2/userinfo';
        $headers = [
            'Authorization: Bearer ' . $accessToken,
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $profileUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $profileResponse = curl_exec($ch);
        curl_close($ch);

        echo $profileResponse;
    } else {
        echo 'Token error';
    }
}
?>

我们的疑问

  • 如何通过OpenID Connect获取这些基础的用户资料字段?
  • 是否需要申请特殊权限或者进行产品审核?

我们原本期望调用/v2/userinfo接口能返回用户的firstName、lastName和emailAddress,但实际要么没有数据,要么请求直接失败,实在找不到问题所在,恳请各位大佬帮忙排查!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 09:39:32