larksuite / node-sdk

larksuite open sdk for nodejs
MIT License
153 stars 31 forks source link

【BUG】发送消息失败 #74

Closed githublaohu closed 9 months ago

githublaohu commented 9 months ago

代码 this.client = new lark.Client({ appId: this.appId, appSecret: this.appSecret, }); 异常 (node:1178221) Warning: Accessing non-existent property 'splice' of module exports inside circular dependency (Usenode --trace-warnings ...` to show where the warning was created) (node:1178221) Warning: Accessing non-existent property 'splice' of module exports inside circular dependency (node:1178221) Warning: Accessing non-existent property 'splice' of module exports inside circular dependency (node:1178221) Warning: Accessing non-existent property 'splice' of module exports inside circular dependency console.error Error: Cross origin http://localhost forbidden at dispatchError (/leaning/work/hyperimagine/kemon-next/node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:63:19) at validCORSHeaders (/leaning/work/hyperimagine/kemon-next/node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5) at validCORSPreflightHeaders (/leaning/work/hyperimagine/kemon-next/node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8) at Request. (/leaning/work/hyperimagine/kemon-next/node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:377:12) at Request.emit (node:events:514:28) at Request.emit (node:domain:488:12) at Request._processResponse (/leaning/work/hyperimagine/kemon-next/node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12) at ClientRequest. (/leaning/work/hyperimagine/kemon-next/node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:101:12) at Object.onceWrapper (node:events:629:26) at ClientRequest.emit (node:events:514:28) at ClientRequest.emit (node:domain:488:12) at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:693:27) at HTTPParser.parserOnHeadersComplete (node:_http_common:119:17) at TLSSocket.socketOnData (node:_http_client:535:22) at TLSSocket.emit (node:events:514:28) at TLSSocket.emit (node:domain:488:12) at addChunk (node:internal/streams/readable:545:12) at readableAddChunkPushByteMode (node:internal/streams/readable:495:3) at TLSSocket.Readable.push (node:internal/streams/readable:375:5) at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) at TLSWrap.callbackTrampoline (node:internal/async_hooks:130:17) { type: 'XMLHttpRequest' }

  at VirtualConsole.<anonymous> (node_modules/.pnpm/jest-environment-jsdom@29.7.0/node_modules/jest-environment-jsdom/build/index.js:63:23)
  at dispatchError (node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:66:53)
  at validCORSHeaders (node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
  at validCORSPreflightHeaders (node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
  at Request.<anonymous> (node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:377:12)
  at Request._processResponse (node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
  at ClientRequest.<anonymous> (node_modules/.pnpm/jsdom@20.0.3/node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:101:12)

console.log

  AxiosError {
    message: 'Network Error',
    name: 'AxiosError',
    code: 'ERR_NETWORK',
    config: {
      transitional: [Object],
      adapter: [Function: xhrAdapter],
      transformRequest: [Array],
      transformResponse: [Array],
      timeout: 0,
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      env: [Object],
      validateStatus: [Function: validateStatus],
      headers: [Object],
      method: 'post',
      url: 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal',
      data: '{"app_id":"cli_a518896598b9d00c","app_secret":"C83uGQGEBPlvE8xcRtckSrkrG7f0t7TS"}'
    },
    request: XMLHttpRequest {}
  }
]

  at Object.error (node_modules/.pnpm/@larksuiteoapi+node-sdk@1.23.0/node_modules/@larksuiteoapi/node-sdk/lib/index.js:56715:17)

Cannot destructure property 'tenant_access_token' of '(intermediate value)' as it is undefined. TypeError: Cannot destructure property 'tenant_access_token' of '(intermediate value)' as it is undefined. at TokenManager. (/next/node_modules/.pnpm/@larksuiteoapi+node-sdk@1.23.0/node_modules/@larksuiteoapi/node-sdk/lib/index.js:56849:21) at Generator.next () at fulfilled (/next/node_modules/.pnpm/@larksuiteoapi+node-sdk@1.23.0/node_modules/@larksuiteoapi/node-sdk/lib/index.js:67:58) at processTicksAndRejections (node:internal/process/task_queues:95:5)

定位1: this.appId , this.appSecret 这两个数据已经传过去了。 const { tenant_access_token, expire } = yield this.httpInstance .post(${this.domain}/open-apis/auth/v3/tenant_access_token/internal, { app_id: this.appId, app_secret: this.appSecret, }) .catch((e) => { this.logger.error(e); }); ` 定位2:

  1. 不是浏览器。不需要跨域检测吧
  2. origin 的内容是默认写入的 function validCORSHeaders(xhr, response, flag, properties, origin) { const acaoStr = response.headers["access-control-allow-origin"]; const acao = acaoStr ? acaoStr.trim() : null; if (acao !== "*" && acao !== origin) { properties.error = "Cross origin " + origin + " forbidden"; dispatchError(xhr); return false; } const acacStr = response.headers["access-control-allow-credentials"]; const acac = acacStr ? acacStr.trim() : null; if (flag.withCredentials && acac !== "true") { properties.error = "Credentials forbidden"; dispatchError(xhr); return false; } return true; }
githublaohu commented 9 months ago

jsdom 库 强制检查了跨域

// don't send the real request if we aren't allowed to use the headers if (!validCORSPreflightHeaders(xhr, resp, flag, properties)) { setResponseToNetworkError(xhr); return; }

mazhe-nerd commented 9 months ago

是字节租户的同学吗?内部对域名做了限制,看一下这篇云文档:字节自建应用调用飞书服务端OpenAPI改造手册

githublaohu commented 9 months ago

可以提供链接吗? Java 可以调用,nodejs 调用失败。

mazhe-nerd commented 9 months ago

可以提供链接吗? Java 可以调用,nodejs 调用失败。

飞书私聊下我?mazhe.nerd

githublaohu commented 9 months ago

只能有手机号 与邮箱添加....

githublaohu commented 9 months ago

或则提供二维码,可以吗?

mazhe-nerd commented 9 months ago

只能有手机号 与邮箱添加....

mazhe.nerd@bytedance.com

githublaohu commented 9 months ago

麻烦,通过下申请好友请求

mazhe-nerd commented 9 months ago

麻烦,通过下申请好友请求

我这边收不到请求;如果不是字节租户的同学上述方法可能不适用;可以提供一个最小复现化demo我这边排查下嘛?

githublaohu commented 9 months ago

有类似的微信沟通群吗?

mazhe-nerd commented 9 months ago

有类似的微信沟通群吗?

没有;通过github issue机制来解决问题。同学先用最小demo复现一下?

githublaohu commented 9 months ago

只是做了简单的非常

class FeishuProvider {
  client: lark.Client;

  constructor() {
    this.client = new lark.Client({
      appId: "cli",
      appSecret: "C",
    });
  }

  sendMessage(receive_id: string, msg_type: string, content: string) {
    this.client.im.message
      .create({
        data: {
          receive_id: receive_id,
          msg_type: msg_type,
          content: content,
        },
        params: {
          receive_id_type: "chat_id",
        },
      })
      .then((data) => {
        console.log("发送成功");
      })
      .catch((e) => {
        console.log("发送失败");
      });
  }
}
const data = {
      service_name: "kemon-next",
      IP: "127.0.0.1",
      start_time: new Date().toLocaleString(),
      start_up_time: 3,
      environment: env.NODE_ENV,
      error: error,
      status: error != undefined ? "success" : "fail",
    };
    feishu.sendMessage(
      "oc",
      "interactive",
      JSON.stringify(data),
    );
mazhe-nerd commented 9 months ago

OK;sdk的版本是1.23.0吧

mazhe-nerd commented 9 months ago

我这边试了一下是OK的,看上面报错是不是系统开了什么代理,导致open.feishu.cn的请求都挂了;可以试下不用sdk,用axios裸调用一下这个获取token的api看看能不能调通?:https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token_internal image

githublaohu commented 9 months ago

最新版本 1.23.0... 还是异常

TypeError: Cannot destructure property 'tenant_access_token' of '(intermediate value)' as it is undefined. at TokenManager.<anonymous> (/leaning/work/temp/kemon/.next/server/chunks/eef6e_@larksuiteoapi_node-sdk_es_index_a9f269.js:51011:21) at Generator.next (<anonymous>) at fulfilled (/leaning/work/temp/kemon/.next/server/chunks/eef6e_@larksuiteoapi_node-sdk_es_index_a9f269.js:86:32) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {stack: "TypeError: Cannot destructure property 'tenant_acc…jections (node:internal/process/task_queues:95:5)", message: "Cannot destructure property 'tenant_access_token' of '(intermediate value)' as it is undefined."}
githublaohu commented 9 months ago

请问没有问题。依赖库进行检查,

githublaohu commented 9 months ago

同的哈。

POST https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal
Content-Type: multipart/form-data

{
  "app_id": "cli",
  "app_secret": "1"
}

返回的结果

{
  "code": 0,
  "expire": 7200,
  "msg": "ok",
  "tenant_access_token": "t-g10435b9BNTGEEOLG5EQHDPCHWZAFJ5DMT4VAWWR"
}
mazhe-nerd commented 9 months ago

奇怪,加一下这个开发者互助交流群,艾特一下mazhe.nerd,群里沟通下: 邀请你加入飞书群,快点击https://go.feishu.cn/join-chat/58fr62a3-ae11-478c-b4fe-e2df2aacbdac加入吧!