Skip to content

Commit ba1a1ca

Browse files
author
yydzxz
committed
授权接口
支持docker compose启动ngrok redis 本项目 docker compose在windows上, 连接redis失败的问题 指定docker redis版本, 防止使用了本地旧的latest版本缓存 新增GlobalExceptionHandler和ByteDanceOpenDemoResponseAdvice,用于统一处理返回值和异常 根据handler处理结果,自动返回success或者failed 运营管理接口示例 支付服务接口示例 全局异常处理 提供一个简便方法 使用4.0.5版本的sdk
1 parent cc463b4 commit ba1a1ca

67 files changed

Lines changed: 2198 additions & 144 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM openjdk:8-jdk-alpine
2+
RUN addgroup -S spring && adduser -S spring -G spring
3+
USER spring:spring
4+
ARG JAR_FILE=target/*.jar
5+
COPY ${JAR_FILE} app.jar
6+
CMD ["java","-jar","/app.jar", "--spring.profiles.active=docker"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [year] [fullname]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,131 @@
11
# ByteDance-Open-Demo
2+
- [字节跳动小程序第三方接入文档](https://microapp.bytedance.com/docs/zh-CN/mini-app/thirdparty/API/update)
3+
- 该demo为[ByteDanceOpen SDK](https://github.com/yydzxz/ByteDanceOpen)用法示例. 大部分参数和url跟字节跳动open api保持相同, 方便对照文档快速理解使用, 剩余的按照RESTful命名风格
4+
- 提供了docker启动方式,包含redis和ngrok服务
5+
- 当前页面上的图片如果裂了,可以[尝试修改host](https://github.com/labuladong/fucking-algorithm/issues/271#issuecomment-620470589),如果还不行那么就需要科学上网o( ̄ヘ ̄o#),或者clone后查看images/gifs文件夹下对应的图片
6+
- 如果觉得用起来还不错╰(●’◡’●)╮,希望能够**star🌟**支持一下~~~
7+
- 如果有接口没有及时更新,可以给我提issue或者PR,着急的话也可以通过sdk暴露的接口自己实现(详情见下面的[其他注意事项](#其他注意事项))。
8+
9+
## 申请账号
10+
- 先在[字节跳动小程序管理后台](https://microapp.bytedance.com)注册一个账号。
11+
- [字节跳动小程序管理后台](https://microapp.bytedance.com)账号自带一个[字节跳动第三方平台](https://open.microapp.bytedance.com)账号(登录小程序管理后台后,进入第三方平台直接就是登录状态),创建一个第三平台后,将第三方平台的相关数据填入
12+
`application-dev.yml`
13+
![image](https://github.com/yydzxz/ByteDance-Open-Demo/blob/dev/images/QQ20200714-122557%402x.png?raw=true)
14+
15+
## 配置ip白名单
16+
-[本机外网ip](https://www.ipaddress.com/)配置到[字节跳动第三方平台](https://open.microapp.bytedance.com)的白名单
17+
- ![image](https://github.com/yydzxz/ByteDance-Open-Demo/blob/dev/images/QQ20200717-210903%402x.png?raw=true)
18+
19+
## 启动项目
20+
### 方式1: docker启动方式
21+
#### 1. 进入项目目录
22+
```bash
23+
cd /yourpath/bytedance-open-demo
24+
```
25+
#### 2. 打包jar
26+
```bash
27+
mvn clean install -DskipTests
28+
```
29+
#### 3. 在**docker-compose.yml**中填入第三方平台的信息
30+
#### 4. 通过docker启动所需服务: redis,ngrok以及本项目
31+
```bash
32+
docker-compose up --build
33+
```
34+
#### 5. 获取本机外网地址: 查询ngrok提供的外网地址
35+
36+
##### Mac
37+
```bash
38+
curl $(docker port bytedance-open-demo_ngrok_1 4040)/api/tunnels
39+
```
40+
##### Windows
41+
- [点击查看外网地址](http://127.0.0.1:4040/api/tunnels)
42+
43+
两种方式都可以从**public_url**中找到所需的外网地址
44+
45+
##### 6. 配置 授权发起页域名,授权接收URL,消息与事件接收URL
46+
- 使用从上一步获取的外网地址, 去[字节跳动第三方平台](https://open.microapp.bytedance.com)配置以下信息:
47+
![image](https://github.com/yydzxz/ByteDance-Open-Demo/blob/dev/images/2431599530574_.pic_hd.jpg?raw=true)
48+
![image](https://github.com/yydzxz/ByteDance-Open-Demo/blob/dev/images/2441599533989_.pic_hd.jpg?raw=true)
49+
50+
### 方式2: 普通启动方式
51+
#### 1. 启动redis
52+
- `access_token`等数据都是保存在redis中,所以需要一个redis服务
53+
54+
- 为了方便使用,项目中提供了一个默认的`redis.conf`, 位于`docker/redis/redis.conf`, 只修改了两个配置:
55+
56+
```bash
57+
# 把protected-mode改为了no
58+
protected-mode no
59+
# 注释了bind
60+
# bind 127.0.0.1 ::1
61+
```
62+
- 本项目使用的redis客户端是[redisson](https://github.com/redisson/redisson),可以直接使用。如果想修改redis连接相关配置,可以在`application-dev.yml`指定redis的配置文件。
63+
64+
- 如果想要使用jedis,可以自己实现一个`IByteDanceRedisOps`
65+
66+
#### 2. 获取本机外网地址: 内网穿透
67+
- 如果没有公网地址,那么需要使用内网穿透工具。比如[ngrok](https://ngrok.com/)
68+
- 如果使用ngrok,下载好后,根据不同的操作系统,在命令行执行
69+
```bash
70+
# MacOS
71+
ngrok http 8080
72+
# Windows
73+
ngrok.exe http 8080
74+
```
75+
就能得到一个映射到8080端口(项目默认启动端口)的外网地址。
76+
77+
#### 3. 配置 授权发起页域名,授权接收URL,消息与事件接收URL
78+
- [同上](#6-配置-授权发起页域名授权接收url消息与事件接收url)
79+
80+
#### 4. 启动本项目
81+
- 启动项目后需要等待字节跳动服务器将ticket推送过来后(一般10分钟以内),才能进行后续的授权等api调用。如果一直没有推送,请到[字节跳动第三方平台](https://open.microapp.bytedance.com),选择自己创建的第三方平台确认推送地址是否配置正确
82+
![image](https://github.com/yydzxz/ByteDance-Open-Demo/blob/dev/images/QQ20200714-130942%402x.png?raw=true)
83+
84+
85+
## 任选一种方式启动项目后,进行接口测试
86+
- 请确定ticket已经推送过来了。如果ticket推送过来,日志中会打印"MsgTypeTicketHandler 开始处理消息: xxxx"
87+
88+
### 网页授权
89+
- 先去[字节跳动小程序管理后台](https://microapp.bytedance.com/app/applist)创建一个小程序
90+
- 然后去[字节跳动第三方平台](https://open.microapp.bytedance.com/tplist)将刚才创建的小程序的appid添加到【授权测试小程序列表】,以便测试
91+
![image](https://github.com/yydzxz/ByteDance-Open-Demo/blob/dev/images/QQ20200717-210508%402x.png?raw=true)
92+
93+
- 浏览器中输入授权地址: [https://你的公网地址或者ngrok生成的公网地址/bytedance/v1/auth/goto_auth_url_show]()
94+
### 模版管理
95+
- [获取第三方平台的草稿列表](http://127.0.0.1:8080/bytedance/v1/template/draft/list)
96+
- [获取第三方平台的模版列表](http://127.0.0.1:8080/bytedance/v1/template/list)
97+
98+
## 事件推送处理
99+
- 可以在`EventHandlerConfiguration`配置用于处理相应事件的handler
100+
101+
## 单元测试
102+
1. [启动项目](#启动项目)
103+
104+
2. [进行小程序授权](#网页授权)
105+
106+
3. 配置`src/test/resources/application-unittest.yml`中的
107+
```yml
108+
bytedance:
109+
open:
110+
componentAppId: ""
111+
componentSecret: ""
112+
componentToken: ""
113+
componentAesKey: ""
114+
```
115+
116+
4. 将刚才授权的小程序appid配置到`src/test/resources/application-unittest.yml`中的
117+
```yml
118+
test-app-info:
119+
appid: ""
120+
```
121+
5. 运行单元测试
122+
123+
124+
## 其他注意事项
125+
- 加解密异常 java.security.InvalidKeyException: Illegal key size
126+
- 因为秘钥长度超过128位,在某些jdk版本中默认只支持128位以下的秘钥
127+
- 解决方案为使用无限制的策略文件替换 $JAVA_HOME\jre\lib\security 下的策略文件
128+
- 下载地址
129+
- jdk8 [https://www.oracle.com/java/technologies/javase-jce8-downloads.html](https://www.oracle.com/java/technologies/javase-jce8-downloads.html)
130+
- 目前字节跳动的[字节跳动开放平台文档](https://bytedance.feishu.cn/docs/doccnYmtnRy6APhKiTfYgW#)还在不断更新,我也会根据他的更新不断新增接口。如果有接口没有及时更新,可以给我提issue或者PR,着急的话也可以通过sdk暴露的接口自己实现。
131+
![image](https://github.com/yydzxz/ByteDance-Open-Demo/blob/dev/images/1407E96CAA9184803B3BF7D53A80649E.jpg?raw=true)

docker-compose.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
version: '3'
2+
services:
3+
web:
4+
build: .
5+
ports:
6+
- "8080:8080"
7+
expose:
8+
- "8080"
9+
depends_on:
10+
- "redis"
11+
- "ngrok"
12+
environment:
13+
- "bytedance.open.componentAppId=your componentAppId"
14+
- "bytedance.open.componentSecret=your componentSecret"
15+
- "bytedance.open.componentToken=your componentToken"
16+
- "bytedance.open.componentAesKey=your componentAesKey"
17+
- "REDIS_HOST=bytedance-open-demo_redis_1"
18+
networks:
19+
- bytedanceNet
20+
redis:
21+
build:
22+
context: ./docker/redis
23+
dockerfile: Dockerfile
24+
ports:
25+
- "6379:6379"
26+
expose:
27+
- "6379"
28+
networks:
29+
- bytedanceNet
30+
ngrok:
31+
image: "wernight/ngrok"
32+
command: ["ngrok", "http", "bytedance-open-demo_web_1:8080"]
33+
ports:
34+
- "4040:4040"
35+
expose:
36+
- "4040"
37+
networks:
38+
- bytedanceNet
39+
40+
networks:
41+
bytedanceNet:

docker/ngrok/Dockerfile

Whitespace-only changes.

docker/ngrok/Readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### [ngork for docker](https://hub.docker.com/r/wernight/ngrok)
2+
3+
4+
#### 查看分配的外网地址
5+
`curl $(docker port bytedance-open-demo_ngrok_1 4040)/api/tunnels`

docker/redis/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM redis:6.0.6
2+
COPY redis.conf /usr/local/etc/redis/redis.conf
3+
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
188 KB
Loading

images/2431599530574_.pic_hd.jpg

113 KB
Loading

0 commit comments

Comments
 (0)