Closed LinLin00000000 closed 1 month ago
import * as lark from '@larksuiteoapi/node-sdk' import 'dotenv/config' import { sleep, unique } from './utils' const appId = process.env.APP_ID as string const appSecret = process.env.APP_SECRET as string const client = new lark.Client({ appId, appSecret }) let n = 10 while (n--) { try { const res = await client.attendance.group.get({ params: { employee_type: 'employee_id', dept_type: 'open_id' }, path: { group_id: '7204658043383136257' } }) console.log(`res.data?.bind_dept_ids: ${res.data?.bind_dept_ids}`) const dept_ids = res.data?.bind_dept_ids // 获取部门下的所有子部门 const departmentIds = await Promise.all((dept_ids || []).map(async (deptId) => { const res = await client.contact.department.children({ params: { user_id_type: 'user_id', fetch_child: true, page_size: 50 }, path: { department_id: deptId } }) return res?.data?.items?.map((item) => item?.open_department_id) })) console.log(`departmentIds: ${unique(departmentIds.flat().filter(Boolean))}`) console.log() } catch (error) { console.error(error) } finally { await sleep(1000) } }
运行结果:
res.data?.bind_dept_ids: od-e336f614e6ae683c502b2494eac14d71,od-d34ac79890e961f68ca64413550a6fd7,od-4de56eef0c08d1b8ecab00fde8114f93 departmentIds: od-80c3588af75e1fd81e748d2c7852b04c,od-82d1419cfa04fc66b88fe806236a4e50,od-971d0c5d80e795fa2f87b7593c083c6e,od-5247b9049dcfe85af357977035e4f2f2,od-a369ab05796dea9060cf2a154aa1eb02 res.data?.bind_dept_ids: undefined departmentIds: res.data?.bind_dept_ids: undefined departmentIds: res.data?.bind_dept_ids: undefined departmentIds: res.data?.bind_dept_ids: od-d34ac79890e961f68ca64413550a6fd7,od-4de56eef0c08d1b8ecab00fde8114f93,od-e336f614e6ae683c502b2494eac14d71 departmentIds: od-facc22996a6614bd7e1634a6526e3dcb,od-4c403981014b2657e07beb2f15e1069c,od-24e14b224d55895f90cd313e17729367,od-68374f8217ab75052c627d6d23df2f10,od-e6097e0a45797db154d47f474bcd6b80,od-80c3588af75e1fd81e748d2c7852b04c,od-82d1419cfa04fc66b88fe806236a4e50,od-971d0c5d80e795fa2f87b7593c083c6e,od-5247b9049dcfe85af357977035e4f2f2,od-a369ab05796dea9060cf2a154aa1eb02 res.data?.bind_dept_ids: undefined departmentIds: res.data?.bind_dept_ids: od-d34ac79890e961f68ca64413550a6fd7,od-4de56eef0c08d1b8ecab00fde8114f93,od-e336f614e6ae683c502b2494eac14d71 departmentIds: od-facc22996a6614bd7e1634a6526e3dcb,od-4c403981014b2657e07beb2f15e1069c,od-24e14b224d55895f90cd313e17729367,od-68374f8217ab75052c627d6d23df2f10,od-e6097e0a45797db154d47f474bcd6b80 res.data?.bind_dept_ids: undefined departmentIds: res.data?.bind_dept_ids: undefined departmentIds: res.data?.bind_dept_ids: od-e336f614e6ae683c502b2494eac14d71,od-d34ac79890e961f68ca64413550a6fd7,od-4de56eef0c08d1b8ecab00fde8114f93 [error]: [ [ { message: 'Request failed with status code 502', config: [Object], request: [Object], response: [Object] } ] ] AxiosError: Request failed with status code 502 at settle (C:\Users\Lin\ws\lark-attendance-notice\node_modules\.pnpm\axios@0.28.1\node_modules\axios\lib\core\settle.js:17:12) at IncomingMessage.handleStreamEnd (C:\Users\Lin\ws\lark-attendance-notice\node_modules\.pnpm\axios@0.28.1\node_modules\axios\lib\adapters\http.js:382:11) at IncomingMessage.emit (node:events:530:35) at IncomingMessage.emit (node:domain:488:12) at endReadableNT (node:internal/streams/readable:1696:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { code: 'ERR_BAD_RESPONSE', config: { transitional: { silentJSONParsing: true, forcedJSONParsing: true, clarifyTimeoutError: false }, adapter: [Function: httpAdapter], transformRequest: [ [Function: transformRequest] ], transformResponse: [ [Function: transformResponse] ], timeout: 0, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, maxBodyLength: -1, env: { FormData: [Function], Blob: [class Blob] }, validateStatus: [Function: validateStatus], headers: { Accept: 'application/json, text/plain, */*', Authorization: 'Bearer t-g10483l8LXHMIU2W3QD3JZV65V', 'User-Agent': 'oapi-node-sdk/1.0.0', 'Content-Type': 'application/json', 'Content-Length': 2, host: 'open.feishu.cn' }, url: 'https://open.feishu.cn/open-apis/contact/v3/departments/od-e336f614e6ae683c502b2494eac14d71/children', method: 'get', data: '{}', params: { user_id_type: 'user_id', fetch_child: true, page_size: 50 } }, request: <ref *1> ClientRequest { _events: [Object: null prototype] { abort: [Function (anonymous)], aborted: [Function (anonymous)], connect: [Function (anonymous)], error: [Function (anonymous)], socket: [Function (anonymous)], timeout: [Function (anonymous)], finish: [Function: requestOnFinish] }, _eventsCount: 7, _maxListeners: undefined, outputData: [], outputSize: 0, writable: true, destroyed: false, _last: false, chunkedEncoding: false, shouldKeepAlive: false, maxRequestsOnConnectionReached: false, _defaultKeepAlive: true, useChunkedEncodingByDefault: false, sendDate: false, _removedConnection: false, _removedContLen: false, _removedTE: false, strictContentLength: false, _contentLength: 2, _hasBody: true, _trailer: '', finished: true, _headerSent: true, _closed: false, socket: Socket { connecting: false, _hadError: false, _parent: null, _host: null, _closeAfterHandlingError: false, _events: [Object], _readableState: [ReadableState], _writableState: [WritableState], allowHalfOpen: false, _maxListeners: undefined, _eventsCount: 7, _sockname: null, _pendingData: null, _pendingEncoding: '', server: null, _server: null, timeout: 5000, parser: null, _httpMessage: [Circular *1], [Symbol(async_id_symbol)]: 1981, [Symbol(kHandle)]: [TCP], [Symbol(lastWriteQueueSize)]: 0, [Symbol(timeout)]: Timeout { _idleTimeout: 5000, _idlePrev: [TimersList], _idleNext: [TimersList], _idleStart: 26731, _onTimeout: [Function: bound ], _timerArgs: undefined, _repeat: null, _destroyed: false, [Symbol(refed)]: false, [Symbol(kHasPrimitive)]: false, [Symbol(asyncId)]: 1980, [Symbol(triggerId)]: 0 }, [Symbol(kBuffer)]: null, [Symbol(kBufferCb)]: null, [Symbol(kBufferGen)]: null, [Symbol(shapeMode)]: true, [Symbol(kCapture)]: false, [Symbol(kSetNoDelay)]: true, [Symbol(kSetKeepAlive)]: true, [Symbol(kSetKeepAliveInitialDelay)]: 60, [Symbol(kBytesRead)]: 0, [Symbol(kBytesWritten)]: 0 }, _header: 'GET https://open.feishu.cn/open-apis/contact/v3/departments/od-e336f614e6ae683c502b2494eac14d71/children?user_id_type=user_id&fetch_child=true&page_size=50 HTTP/1.1\r\n' + 'Accept: application/json, text/plain, */*\r\n' + 'Authorization: Bearer t-g10483l8LXHMIU6WSSJZV65V\r\n' + 'User-Agent: oapi-node-sdk/1.0.0\r\n' + 'Content-Type: application/json\r\n' + 'Content-Length: 2\r\n' + 'host: open.feishu.cn\r\n' + 'Connection: keep-alive\r\n' + '\r\n', _keepAliveTimeout: 0, _onPendingData: [Function: nop], agent: Agent { _events: [Object: null prototype], _eventsCount: 2, _maxListeners: undefined, defaultPort: 80, protocol: 'http:', options: [Object: null prototype], requests: [Object: null prototype] {}, sockets: [Object: null prototype], freeSockets: [Object: null prototype] {}, keepAliveMsecs: 1000, keepAlive: true, maxSockets: Infinity, maxFreeSockets: 256, scheduling: 'lifo', maxTotalSockets: Infinity, totalSocketCount: 1, [Symbol(shapeMode)]: false, [Symbol(kCapture)]: false }, socketPath: undefined, method: 'GET', maxHeaderSize: undefined, insecureHTTPParser: undefined, joinDuplicateHeaders: undefined, path: 'https://open.feishu.cn/open-apis/contact/v3/departments/od-e336f614e6ae683c502b2494eac14d71/children?user_id_type=user_id&fetch_child=true&page_size=50', _ended: true, res: IncomingMessage { _events: [Object], _readableState: [ReadableState], _maxListeners: undefined, socket: [Socket], httpVersionMajor: 1, httpVersionMinor: 1, httpVersion: '1.1', complete: true, rawHeaders: [Array], rawTrailers: [], joinDuplicateHeaders: undefined, aborted: false, upgrade: false, url: '', method: null, statusCode: 502, statusMessage: 'Bad Gateway', client: [Socket], _consuming: false, _dumped: false, req: [Circular *1], _eventsCount: 4, responseUrl: 'https://open.feishu.cn/open-apis/contact/v3/departments/od-e336f614e6ae683c502b2494eac14d71/children?user_id_type=user_id&fetch_child=true&page_size=50', redirects: [], [Symbol(shapeMode)]: true, [Symbol(kCapture)]: false, [Symbol(kHeaders)]: [Object], [Symbol(kHeadersCount)]: 4, [Symbol(kTrailers)]: null, [Symbol(kTrailersCount)]: 0 }, aborted: false, timeoutCb: [Function: emitRequestTimeout], upgradeOrConnect: false, parser: null, maxHeadersCount: null, reusedSocket: false, host: '127.0.0.1', protocol: 'http:', _redirectable: Writable { _events: [Object], _writableState: [WritableState], _maxListeners: undefined, _options: [Object], _ended: true, _ending: true, _redirectCount: 0, _redirects: [], _requestBodyLength: 2, _requestBodyBuffers: [], _eventsCount: 3, _onNativeResponse: [Function (anonymous)], _currentRequest: [Circular *1], _currentUrl: 'https://open.feishu.cn/open-apis/contact/v3/departments/od-e336f614e6ae683c502b2494eac14d71/children?user_id_type=user_id&fetch_child=true&page_size=50', [Symbol(shapeMode)]: true, [Symbol(kCapture)]: false }, [Symbol(shapeMode)]: false, [Symbol(kCapture)]: false, [Symbol(kBytesWritten)]: 0, [Symbol(kNeedDrain)]: false, [Symbol(corked)]: 0, [Symbol(kOutHeaders)]: [Object: null prototype] { accept: [Array], authorization: [Array], 'user-agent': [Array], 'content-type': [Array], 'content-length': [Array], host: [Array] }, [Symbol(errored)]: null, [Symbol(kHighWaterMark)]: 16384, [Symbol(kRejectNonStandardBodyWrites)]: false, [Symbol(kUniqueHeaders)]: null }, response: { status: 502, statusText: 'Bad Gateway', headers: { connection: 'close', 'content-length': '0' }, config: { transitional: [Object], adapter: [Function: httpAdapter], transformRequest: [Array], transformResponse: [Array], timeout: 0, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, maxBodyLength: -1, env: [Object], validateStatus: [Function: validateStatus], headers: [Object], url: 'https://open.feishu.cn/open-apis/contact/v3/departments/od-e336f614e6ae683c502b2494eac14d71/children', method: 'get', data: '{}', params: [Object] }, request: <ref *1> ClientRequest { _events: [Object: null prototype], _eventsCount: 7, _maxListeners: undefined, outputData: [], outputSize: 0, writable: true, destroyed: false, _last: false, chunkedEncoding: false, shouldKeepAlive: false, maxRequestsOnConnectionReached: false, _defaultKeepAlive: true, useChunkedEncodingByDefault: false, sendDate: false, _removedConnection: false, _removedContLen: false, _removedTE: false, strictContentLength: false, _contentLength: 2, _hasBody: true, _trailer: '', finished: true, _headerSent: true, _closed: false, socket: [Socket], _header: 'GET https://open.feishu.cn/open-apis/contact/v3/departments/od-e336f614e6ae683c502b2494eac14d71/children?user_id_type=user_id&fetch_child=true&page_size=50 HTTP/1.1\r\n' + 'Accept: application/json, text/plain, */*\r\n' + 'Authorization: Bearer t-g10483l8LXHMISS3JZV65V\r\n' + 'User-Agent: oapi-node-sdk/1.0.0\r\n' + 'Content-Type: application/json\r\n' + 'Content-Length: 2\r\n' + 'host: open.feishu.cn\r\n' + 'Connection: keep-alive\r\n' + '\r\n', _keepAliveTimeout: 0, _onPendingData: [Function: nop], agent: [Agent], socketPath: undefined, method: 'GET', maxHeaderSize: undefined, insecureHTTPParser: undefined, joinDuplicateHeaders: undefined, path: 'https://open.feishu.cn/open-apis/contact/v3/departments/od-e336f614e6ae683c502b2494eac14d71/children?user_id_type=user_id&fetch_child=true&page_size=50', _ended: true, res: [IncomingMessage], aborted: false, timeoutCb: [Function: emitRequestTimeout], upgradeOrConnect: false, parser: null, maxHeadersCount: null, reusedSocket: false, host: '127.0.0.1', protocol: 'http:', _redirectable: [Writable], [Symbol(shapeMode)]: false, [Symbol(kCapture)]: false, [Symbol(kBytesWritten)]: 0, [Symbol(kNeedDrain)]: false, [Symbol(corked)]: 0, [Symbol(kOutHeaders)]: [Object: null prototype], [Symbol(errored)]: null, [Symbol(kHighWaterMark)]: 16384, [Symbol(kRejectNonStandardBodyWrites)]: false, [Symbol(kUniqueHeaders)]: null }, data: '' } }
但是如果我直接使用原生的 fetch 发出 http 请求就没有任何问题,调用上百次都是返回一致的结果。
我发现好像是代理的问题,把代理关了就正常了
运行结果:
但是如果我直接使用原生的 fetch 发出 http 请求就没有任何问题,调用上百次都是返回一致的结果。