Maui Blazor 中文社区 QQ群:645660665

Tesla 开发者 API 指南:通过Http发送命令

前言

特斯拉提供两种与汽车通信的方式。一种是使用 API 通过互联网,另一种是使用 BLE 连接。

特斯拉现在只能接受车辆命令 SDK (vehicle command SDK)方式发送命令,该 SDK 使用 Http-Proxy 服务器将命令转发给车辆。除了验证 oAuth 令牌之外,特斯拉正在转向一种更安全的方式将命令传递给汽车.

需要注意的是,网络API会有一点延时,比 BLE 方式稍微慢一点.

先决条件

  1. Golang 已安装
  2. 在 Tesla 门户注册第三方应用时生成的公钥和私钥可用
  3. 有效的 Tesla 用户凭证,用于生成访问令牌

go 环境搭建自行搜索教程

注意:本工具虽然标记只能运行于 mac 或者 linux, 但是win下也是能运行的,只是官方没有技术支援而以。

vin码就是车架识别号,类似 7ABCGDEE123ABC555。 在前玻璃地下那里能看到,手机app也能找到。以下用 {yourvin} 代替,实际替换为你自己的字符串,{ } 这两个符号也去掉。

1. 克隆项目到本地

https://github.com/teslamotors/vehicle-command.git

2. 项目根目录下执行命令

go get ./...
go build ./...
go install ./...
cd cmd
cd tesla-http-proxy 
go build 

3. 生成密钥

为了演示方便,直接就在当前目录下建立密钥文件

生成私钥和公钥

openssl req -x509 -nodes -newkey ec \
-pkeyopt ec_paramgen_curve:secp521r1 \
-pkeyopt ec_param_enc:named_curve \
-subj '/CN=localhost' \
-keyout key.pem -out cert.pem -sha256 -days 3650 \
-addext "extendedKeyUsage = serverAuth" \
-addext "keyUsage = digitalSignature, keyCertSign, keyAgreement"

key.pem是私钥文件。cert.pem 是TLS证书链文件

3. 启动 HTTP 代理服务器

private.pem 是在使用 tesla 注册第三方应用程序时生成的私钥

./tesla-http-proxy -tls-key key.pem -key-file private.pem -cert cert.pem -port 443 -verbose

4. 测试闪灯命令

{AuthorizationToken} 来源于文章注册第三方应用程序生成的 Token, 类似于 eyJhbGci...lecxsFTYOE3n3w

curl
--cacert cert.pem \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer {AuthorizationToken}" \
--data '{}' \
"https://localhost:443/api/1/vehicles/7SAYGDEE2PA110666/command/flash_lights"

更多

  1. Tesla 开发者 API 指南:注册开发者账号
  2. Tesla 开发者 API 指南:BLE 发送车辆命令
  3. Tesla 开发者 API 指南:通过Http发送命令
  4. Tesla 开发者 API 指南:Tesla Fleet API

参考资料

https://shankarkumarasamy.blog/2024/02/25/tesla-developer-api-guide-http-proxy-server-integration-part-4/

https://github.com/teslamotors/vehicle-command/blob/main/README.md

posted @ 2024-08-24 17:53  AlexChow  阅读(66)  评论(0编辑  收藏  举报