package com.ylbtech.common.utils.miniprogram;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
/**
* 接口调用凭证
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
*/
public class Auth {
/**
* 获取小程序全局唯一后台接口调用凭据(access_token)
* @param appID 小程序唯一凭证,即 AppID,可在「微信公众平台 - 设置 - 开发设置」页中获得。(需要已经成为开发者,且帐号没有异常状态)
* @param secret 小程序唯一凭证密钥,即 AppSecret,获取方式同 appid
* @param restTemplate
* @return
*/
public static String getAccessToken(String appID, String secret, RestTemplate restTemplate) {
String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
String path = String.format("%s&appid=%s&secret=%s", accessTokenUrl, appID, secret);
ResponseEntity<String> entity = restTemplate.getForEntity(path, String.class);
JSONObject responseObj = JSON.parseObject(entity.getBody());
String access_token = responseObj.getString("access_token");
return access_token;
}
}