认证
请求业务接口,都需要携带token
接口地址
POST
/oauth/token?grant_type=client_credentials&client_secret=7026bf6193f64321aee42ab7115bc382&client_id=edianyun
参数
参数名 | 是否必须 | 类型 | 长度 | 说明 |
---|---|---|---|---|
grant_type | 是 | string | 20 | 固定值:client_credentials |
client_id | 是 | string | 50 | |
client_secret | 是 | string | 50 |
响应
{
"code":0,
"data":{
"accessToken":"6beb924b-7991-402c-b07b-981184fbcb94",
"expiresIn":86196
},
"msg":"成功"
}
携带token请求业务接口
header ‘Authorization: Bearer 6beb924b-7991-402c-b07b-981184fbcb94’
比如:
connection.setRequestProperty("Authorization", "Bearer " + token);
token 获取和存储方法示例:
private String getToken() {
String token= redisUtil.get("dayu:token");
if (token != null && !"".equals(token.trim())) {
return token;
} else {
//此处调用接口获取到token对象
Result<OAuthToken> oAuthTokenResult=dayuOrderService.getToken();
System.out.println(JSON.toJSONString(oAuthTokenResult));
//获取token成功
if(oAuthTokenResult.success()){
OAuthToken oAuthToken=oAuthTokenResult.getData();
token =oAuthToken.getAccessToken();
long expiresIn = oAuthToken.getExpiresIn();
//设置token及有效期
redisUtil.set("dayu:token", token,tokenExpires_in-60);
}
}
return token;
}
public Result<OAuthToken> getToken() {
JSONObject param = new JSONObject();
param.put("grant_type", "client_credentials");
param.put("client_id", this.clientId);
param.put("client_secret", this.clientSecret);
String resp = HttpClientUtil.sendPostForm(this.domain + "/oauth/token", param, (String)null);
return "".equals(resp) ? ResultUtil.failure("获取token失败!请确认参数是否正确") : ResultUtil.success(JSON.parseObject(resp, OAuthToken.class));
}
测试环境:
http://uat.kf.ai:7310
client_id:edianyun
client_secret:7026bf6193f64321aee42ab7115bc382
正式环境域名:
swagger 使用
地址
http://uat.kf.ai:7310/swagger-ui.html#/edianyun
配置Auth
- 配置提供的:ClientId和Secret
文档更新时间: 2024-09-05 13:47 作者:大鱼研发