开发环境下Next.js API接口Axios GET请求出现ETIMEDOUT错误(生产环境正常)
开发环境Next.js调用LinkedIn API遭遇ETIMEDOUT错误
仅在开发环境调用LinkedIn用户信息接口时出现ETIMEDOUT超时错误,生产环境无异常,相关代码及错误信息如下:
代码实现
1. 客户端API调用
const subId = await axios.get( `${process.env.NEXT_PUBLIC_BASE_URL}/auth/linkedin-access/fetch/sub-id?access_token=${user?.linkedin_access_token}` );
2. API路由文件 /auth/linkedin-access/fetch/sub-id/route.ts
import { fetchSubId } from "@/utils/linkedin-api-handlers/fetch-sub-id"; export async function GET(req: NextRequest) { try { const { searchParams } = new URL(req.url); const access_token = searchParams.get("access_token"); if (!access_token) { return NextResponse.json( { path: "/auth/linkedin-access/fetch/sub-id/route", error: "no access token", }, { status: 401, statusText: "access_token not present" } ); } const response = await fetchSubId(access_token); return NextResponse.json({ sub: response }); } catch (e) { return NextResponse.json( { path: "/auth/linkedin-access/fetch/sub-id/route", error: e, }, { status: 500 } ); } }
3. LinkedIn API请求函数 fetchSubId
export async function fetchSubId(accessToken: string): Promise<string> { const req = axios.create({ headers: { Authorization: `Bearer ${accessToken}`, Connection: "keep-alive", }, }); const response = await req.get("https://api.linkedin.com/v2/userinfo"); return response.data.sub; }
错误响应内容
{ "path": "/auth/linkedin-access/fetch/sub-id/route", "error": { "message": "", "name": "AggregateError", "stack": "AggregateError\n at AxiosError.from (webpack-internal:///(rsc)/./node_modules/axios/lib/core/AxiosError.js:85:16)\n at RedirectableRequest.handleRequestError (webpack-internal:///(rsc)/./node_modules/axios/lib/adapters/http.js:513:81)\n at RedirectableRequest.emit (node:events:524:35)\n at eventHandlers.<computed> (webpack-internal:///(rsc)/./node_modules/follow-redirects/index.js:21:28)\n at ClientRequest.emit (node:events:512:28)\n at TLSSocket.socketErrorListener (node:_http_client:495:9)\n at TLSSocket.emit (node:events:512:28)\n at emitErrorNT (node:internal/streams/destroy:151:8)\n at emitErrorCloseNT (node:internal/streams/destroy:116:3)\n at process.processTicksAndRejections (node:internal/process/task_queues:82:21)", "config": { "transitional": { "silentJSONParsing": true, "forcedJSONParsing": true, "clarifyTimeoutError": false }, "adapter": [ "xhr", "http" ], "transformRequest": [ null ], "transformResponse": [ null ], "timeout": 60000, "xsrfCookieName": "XSRF-TOKEN", "xsrfHeaderName": "X-XSRF-TOKEN", "maxContentLength": -1, "maxBodyLength": -1, "env": {}, "headers": { "Accept": "application/json, text/plain, */*", "Authorization": "Bearer XXXXX", "Connection": "keep-alive", "User-Agent": "axios/1.4.0", "Accept-Encoding": "gzip, compress, deflate, br" }, "method": "get", "url": "https://api.linkedin.com/v2/userinfo" }, "code": "ETIMEDOUT", "status": null } }
排查修复建议
- 网络连通性测试:直接用curl或Postman在本地请求
https://api.linkedin.com/v2/userinfo,确认本地网络能正常访问该接口 - 调整超时与重试:延长Axios超时时间,同时添加重试机制,避免单次请求超时
- 开发环境代理配置:检查
next.config.js中的代理规则,确保未拦截LinkedIn API请求,可添加规则放行该域名 - 本地安全软件排查:临时关闭防火墙、杀毒软件,确认是否阻止了Node.js进程的对外请求
- 强制切换Axios适配器:开发环境下强制使用http适配器替代xhr,修改
fetchSubId函数:export async function fetchSubId(accessToken: string): Promise<string> { const req = axios.create({ headers: { Authorization: `Bearer ${accessToken}`, Connection: "keep-alive", }, adapter: require('axios/lib/adapters/http') }); const response = await req.get("https://api.linkedin.com/v2/userinfo"); return response.data.sub; }
内容的提问来源于stack exchange,提问作者Shourya Shikhar
相关产品推荐
相关产品推荐

