共享 API 文档
先取 AppToken
写接口的前置步骤:拿到 appToken,后续请求放进 X-App-Token。
走应用放行时,先用平台分配的 appId 换 appToken。该接口本身豁免网关放行,不需要带任何鉴权头。
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
appId |
string | 是 | 平台下发的网关应用 ID,未登记返回 10003 |
{ "code": 0, "msg": "ok", "data": { "appToken": "xxxx", "exp": 7200, "expireAt": 1760000000 } }
exp是剩余秒数;永久授权时为true。expireAt是绝对过期时间戳;永久授权时为0。- 本地缓存,提前 5 分钟刷新;业务接口返回
10403时重取一次,不要无限重试。 10004授权信息错误、10005授权已过期,说明应用未正确登记或已失效。
安全:换取动作只在你方服务端完成,
appToken由服务端缓存并中转给自己的业务端;不要把appToken写进前端 / 小程序 / APP 代码,也不要下发到浏览器。前端示例中的占位符仅表示「由你方服务端注入的值」,不是让你在客户端保存凭据。
调用示例
curl -sS -X POST "https://api.micro.lck.yn.cn/app/getAppToken" \ -H "Content-Type: application/json" \
-d '{"appId":"平台下发的appId"}'<?php
$body = '{"appId":"平台下发的appId"}';
$headers = [
'Content-Type: application/json',
];
$ctx = stream_context_create(['http' => [
'method' => 'POST',
'header' => implode("\r\n", $headers) . "\r\n",
'content' => $body,
'timeout' => 10,
]]);
$resp = file_get_contents('https://api.micro.lck.yn.cn/app/getAppToken', false, $ctx);
if ($resp === false) { fwrite(STDERR, "request failed\n"); exit(1); }
echo $resp, PHP_EOL;const res = await fetch('https://api.micro.lck.yn.cn/app/getAppToken', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: '{"appId":"平台下发的appId"}'
});
if (!res.ok) throw new Error('HTTP ' + res.status);
console.log(await res.text());import os, urllib.request
req = urllib.request.Request(
'https://api.micro.lck.yn.cn/app/getAppToken',
data='{"appId":"平台下发的appId"}'.encode('utf-8'),
headers={
'Content-Type': 'application/json',
},
method='POST',
)
with urllib.request.urlopen(req, timeout=10) as resp:
print(resp.read().decode('utf-8'))package main
import (
"fmt"
"io"
"net/http"
"strings"
"os"
)
func main() {
req, err := http.NewRequest("POST", "https://api.micro.lck.yn.cn/app/getAppToken", strings.NewReader(`{"appId":"平台下发的appId"}`))
if err != nil { panic(err) }
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil { panic(err) }
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
fmt.Println(string(data))
}import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Demo {
public static void main(String[] args) throws Exception {
HttpRequest req = HttpRequest.newBuilder(URI.create("https://api.micro.lck.yn.cn/app/getAppToken"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"appId\":\"平台下发的appId\"}"))
.build();
HttpResponse<String> res = HttpClient.newHttpClient()
.send(req, HttpResponse.BodyHandlers.ofString());
System.out.println(res.body());
}
}// 换取 appToken 的接口:请求体只带 appId,不需要任何鉴权头。
// 安全:拿到的 appToken 由服务端缓存中转,不要下发到小程序。
const APP_ID = '平台下发的appId' // 仅作为请求体参数,不是鉴权头
wx.request({
url: 'https://api.micro.lck.yn.cn/app/getAppToken',
method: 'POST',
data: {"appId":"平台下发的appId"},
header: {
'content-type': 'application/json',
},
success(res) { console.log(res.data) },
fail(err) { console.error(err) }
})// Flutter:pubspec.yaml 加 http 依赖。
// 换取 appToken 的接口:请求体只带 appId,不需要任何鉴权头;appToken 由服务端缓存。
const appId = '平台下发的appId' // 请求体参数,不是鉴权头
import 'package:http/http.dart' as http;
Future<void> main() async {
final res = await http.post(
Uri.parse('https://api.micro.lck.yn.cn/app/getAppToken'),
headers: {
'Content-Type': 'application/json',
},
body: r'{"appId":"平台下发的appId"}',
);
print(res.body);
}