出现错误的场景是在uniapp的云函数(node8)中,本质是node8的request

uniapp云函数发送请求使用了https://github.com/node-modules/urllib,更多配置项可参考此处或见官网文档。

完整的错误内容如下:

第一种错误

[chatGPT-v1/ac1cd3621670846530977174733/925ms/ERROR] certificate has expired, POST https://api.openai.com/v1/completions -1 (connected: true, keepalive socket: false, agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":0,"errorSocketCount":1,"timeoutSocketCount":0,"requestCount":0,"freeSockets":{},"sockets":{"api.openai.com:443:::::::::TLSv1_2_method":1},"requests":{}}, socketHandledRequests: 1, socketHandledResponses: 0)
headers: {}
Error: certificate has expired
    at TLSSocket.<anonymous> (_tls_wrap.js:1116:38)
    at emitNone (events.js:106:13)
    at TLSSocket.emit (events.js:208:7)
    at TLSSocket._finishInit (_tls_wrap.js:643:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:473:38)

解决办法

调整 rejectUnauthorized 选项来允许 https 证书无效,在node/uniapp发送请求配置项中添加

rejectUnauthorized: false

参考:https://nodejs.org/api/tls.html#tlscreateserveroptions-secureconnectionlistener

第二种错误

[chatGPT-v1/ac1cd3651670844896047115674/707ms/ERROR] write EPROTO 140204022613824:error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version:../deps/openssl/openssl/ssl/s23_clnt.c:802:
, GET https://api.openai.com/v1/completions -1 (connected: true, keepalive socket: false, agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":0,"errorSocketCount":1,"timeoutSocketCount":0,"requestCount":0,"freeSockets":{},"sockets":{"api.openai.com:443:::::::false::":1},"requests":{}}, socketHandledRequests: 1, socketHandledResponses: 0)
headers: {}
Error: write EPROTO 140204022613824:error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version:../deps/openssl/openssl/ssl/s23_clnt.c:802:

    at WriteWrap.afterWrite [as oncomplete] (net.js:868:14)

解决办法

强制 SSL 版本,在node/uniapp发送请求配置项中添加

secureProtocol: 'TLSv1_2_method'

参考:https://nodejs.org/api/tls.html#tlscreatesecurecontextoptions 中的 secureProtocol 配置项

其值可以是

const SSL_METHOD *TLS_method(void);
Constructor for the version-flexible SSL_METHOD structure for clients, servers or both. See SSL_CTX_new(3) for details.

const SSL_METHOD *TLS_client_method(void);
Constructor for the version-flexible SSL_METHOD structure for clients. Must be used to support the TLSv1.3 protocol.

const SSL_METHOD *TLS_server_method(void);
Constructor for the version-flexible SSL_METHOD structure for servers. Must be used to support the TLSv1.3 protocol.

const SSL_METHOD *TLSv1_2_method(void);
Constructor for the TLSv1.2 SSL_METHOD structure for clients, servers or both.

const SSL_METHOD *TLSv1_2_client_method(void);
Constructor for the TLSv1.2 SSL_METHOD structure for clients.

const SSL_METHOD *TLSv1_2_server_method(void);
Constructor for the TLSv1.2 SSL_METHOD structure for servers.

const SSL_METHOD *TLSv1_1_method(void);
Constructor for the TLSv1.1 SSL_METHOD structure for clients, servers or both.

const SSL_METHOD *TLSv1_1_client_method(void);
Constructor for the TLSv1.1 SSL_METHOD structure for clients.

const SSL_METHOD *TLSv1_1_server_method(void);
Constructor for the TLSv1.1 SSL_METHOD structure for servers.

const SSL_METHOD *TLSv1_method(void);
Constructor for the TLSv1 SSL_METHOD structure for clients, servers or both.

const SSL_METHOD *TLSv1_client_method(void);
Constructor for the TLSv1 SSL_METHOD structure for clients.

const SSL_METHOD *TLSv1_server_method(void);
Constructor for the TLSv1 SSL_METHOD structure for servers.

const SSL_METHOD *SSLv3_method(void);
Constructor for the SSLv3 SSL_METHOD structure for clients, servers or both.

const SSL_METHOD *SSLv3_client_method(void);
Constructor for the SSLv3 SSL_METHOD structure for clients.

const SSL_METHOD *SSLv3_server_method(void);
Constructor for the SSLv3 SSL_METHOD structure for servers.

参考:https://www.openssl.org/docs/man1.1.1/man7/ssl.html#Dealing-with-Protocol-Methods

最后修改:2022 年 12 月 12 日
如果觉得我的文章对你有用,奖励一杯咖啡吧!