Java-Class-Miniprogram:com.common.utils.miniprogram.Auth.java

ylbtech-Java-Class-miniprogram:com.common.utils.miniprogram.Auth.java
1.返回顶部
1.1、
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;
    }
}
1.2、
2.返回顶部
 
3.返回顶部
 
4.返回顶部
 
5.返回顶部
 
 
6.返回顶部
 
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/storebook/p/11082833.html