添加测试小程序code

This commit is contained in:
2021-07-08 16:16:39 +08:00
parent 0aa8ae8bdf
commit 1824bdb02a
5 changed files with 75 additions and 1 deletions
@@ -24,4 +24,7 @@ public interface WeChatAuthService {
*/
WeChatAuthServiceImpl.WeChatWebToken getWeChatWebToken(String code);
WeChatAuthServiceImpl.WeChatWebToken getWeChatWebToken(String code, String appId, String appSecret);
}
@@ -108,6 +108,40 @@ public class WeChatAuthServiceImpl implements WeChatAuthService{
}
public WeChatWebToken getWeChatWebToken(String code, String appId, String appSecret){
if(code == null){
return null;
}
HttpRequest request = new HttpRequest();
Map<String, String> params = new HashMap<String, String>();
params.put("appid", appId);
params.put("grant_type", "authorization_code");
params.put("secret", appSecret);
params.put("code", code);
try {
JSONObject json = request.getJsonContent(WE_CHAT_API_HOST + "/sns/oauth2/access_token",params ,"UTF-8");
if(json.containsKey("access_token")){
WeChatWebToken token = new WeChatWebToken();
token.accessToken = json.getString("access_token");
token.expiresIn = json.getInt("expires_in");
token.refreshToken= json.getString("refresh_token");
token.openid = json.getString("openid");
token.scope = json.getString("scope");
return token;
}else{
logger.error("Web Access Token 获取失败:" + json.toString());
}
} catch (HttpRequestException e) {
logger.error("Web Access Token 获取失败:", e);
}
return null;
}
public static class WeChatToken implements Serializable{