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

NextAuth集成LinkedIn OAuth登录后跳转空白页且LinkedIn Analytics无API调用记录的问题求助

NextAuth集成LinkedIn OAuth登录后跳转空白页且LinkedIn Analytics无API调用记录的问题求助

各位大佬好,我最近在尝试用TypeScript实现基于LinkedIn的NextAuth认证,但卡了好久一直没解决问题。现在的情况是:登录选项能正常显示,点击LinkedIn登录后授权流程走完,结果跳转回应用时直接显示空白页面,而且LinkedIn开发者后台的Analytics里完全看不到任何API调用记录,根本不知道哪里出问题了。

问题具体表现

  • 点击LinkedIn登录按钮后,能正常跳转到LinkedIn授权页面,输入账号密码授权后会跳转回我的应用
  • 跳转回应用后页面是空白状态,控制台也没看到明显报错提示
  • LinkedIn开发者后台的Analytics板块里没有任何API请求记录,连获取token或用户信息的请求都没有触发

我的NextAuth配置代码(pages/api/auth/[...nextauth].ts)

import NextAuth from "next-auth";
import type { NextAuthOptions } from "next-auth";

const authOptions: NextAuthOptions = {
  providers: [
    {
      id: "linkedin",
      name: "LinkedIn",
      type: "oauth",
      version: "2.0",
      idToken: true,
      authorization: {
        url: "https://www.linkedin.com/oauth/v2/authorization",
        params: {
          scope: "openid profile email",
          response_type: "code",
        },
      },
      token: {
        url: "https://www.linkedin.com/oauth/v2/accessToken",
        async request(context) {
          const { params } = context;
          // ✅ Declare constants outside the URLSearchParams block
          const hardcodedClientId = "44ddgfdsgdsgsdfdssdfu";
          const hardcodedClientSecret = "WPL_AP1.fefefsdfsaYdada5.qyKbDw==";

          const body = new URLSearchParams({
            grant_type: "authorization_code",
            code: params.code!,
            // redirect_uri: "http://localhost:3000/api/auth/callback/linkedin",
            redirect_uri: "https://28fa-2401-4900-7085-a56e-8de6-d45c-2e3f-e535.ngrok-free.app/api/auth/callback/linkedin",
            client_id: hardcodedClientId,
            client_secret: hardcodedClientSecret,
            code_verifier: params.code_verifier!,
          });

          const res = await fetch("https://www.linkedin.com/oauth/v2/accessToken", {
            method: "POST",
            headers: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
            body: body.toString(),
          });

          if (!res.ok) {
            const errorText = await res.text();
            console.error("❌ LinkedIn Token Exchange Error (Raw):", errorText);
            console.error("🔍 Sent Client ID:", hardcodedClientId);
            console.error("🔍 Sent Client Secret:", hardcodedClientSecret ? "✅ present" : "❌ missing");
            throw new Error("Token exchange failed");
          }
          return res.json();
        },
      },
      userinfo: {
        url: "https://api.linkedin.com/v2/userinfo",
      },
      profile(profile) {
        return {
          id: profile.sub,
          name: profile.name || `${profile.given_name} ${profile.family_name}`,
          email: profile.email,
          image: profile.picture,
        };
      },
      clientId: "77thyyt4uur7nu",
      clientSecret: "WPL_AP1.fefefsdfsaYdada5.qyKbDw==",
      checks: ["pkce", "state"],
    },
  ],
  secret: "fHXNb4aKPzlDDugJBzxzXZ0781Z/tdsGZtxxxsderUo==",
  debug: true,
};

export default NextAuth(authOptions);

我的LinkedIn开发者后台配置

  • OAuth 2.0设置:
    • Access Token有效期:2个月(5184000秒)
  • 授权重定向URI:https://28fa-2401-4900-7085-a56e-8de6-d45c-2e3f-e535.ngrok-free.app/api/auth/callback/linkedin
  • 已申请的OAuth 2.0权限:openid、profile、email、w_member_social
  • 已关联的产品:
    • Share on LinkedIn(默认 tier)
    • Sign In with LinkedIn using OpenID Connect(标准 tier)

我已经检查了重定向URI是否前后端完全一致,也确认了代码里的Client ID和Secret是正确的,但就是不知道为什么跳转后页面空白,而且连LinkedIn的API调用都没触发。有没有大佬能帮忙看看哪里配置错了?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 10:54:53