【Java 微信公众号】③ access_token

简介: 调用微信官方的某些接口必须携带【access_token】作为参数目前 access_token 的有效期为2个小时不要反复获取 access_tokenaccess_token 是供后台(如:Java)调用微信官方接口的

一、access_token

(1) 关于 access_token:

https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html

  • 调用微信官方的某些接口必须携带【access_token】作为参数
  • 目前 access_token 的有效期为2个小时
  • 不要反复获取 access_token
  • access_token 是供后台(如:Java)调用微信官方接口的

(2) 获取 access_token

  • 往微信官方的 GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET 请求路径发送一个 GET 请求
  • 需携带 APPID(应用的唯一标识) 和 APPSECRET(应用秘钥) 作为参数 APPID 和 APPSECRET 可登录自己小程序或公众号平台查看
  • access_token 需保存在后台
  • Java 后台发送 HTTP 网络请求【使用 Spring 提供的一个 HTTP 请求工具:RestTemplate
  • JSON 格式转换:使用 【hutool-all】
  <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>5.8.1</version>
  </dependency>
@SuppressWarnings("all")
public class GqWechatUtil {

    private static final String APP_ID = "wxc436706a34c9dcd8";
    private static final String APP_SECRET = "7fe003e0a896420042307f567c58144a";

    // Spring 提供的发送 HTTP 请求的工具
    private static RestTemplate restTemplate = new RestTemplate();

    private static final String ACCESS_TOKEN = "access_token";
    private static final String EXPIRES_IN = "expires_in";

    private static AccessTokenExpiresIn accessTokenExpiresIn;

    /**
     * 各种请求路径
     */
    private static String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=gqAPPID&secret=gqAPPSECRET";

    /**
     * 【方法】获取 access_token 以及 expires_in
     */
    private static void getAccessTokenExpiresIn() {
        HashMap<String, String> retMap = new HashMap<>();
        // 给请求 url 设值
        String url = accessTokenUrl.replace("gqAPPID", APP_ID).replace("gqAPPSECRET", APP_SECRET);
        // 向 url 发送网络请求, 获得返回值字符串
        String resultStr = restTemplate.getForObject(url, String.class);
        // 把 resultStr 转换为 JSON 格式
        JSONObject resultJSON = JSONUtil.parseObj(resultStr);

        String accessToken = resultJSON.get(ACCESS_TOKEN).toString(); // 凭证值
        String expiresIn = resultJSON.get(EXPIRES_IN).toString(); // 凭证有效时间

        accessTokenExpiresIn = new AccessTokenExpiresIn(accessToken, expiresIn);
    }

    /**
     * 【方法】获取 access_token
     *
     * @return access_token 值
     */
    public static String getAccessToken() {
        if (accessTokenExpiresIn == null || accessTokenExpiresIn.alreadyExpired())
            getAccessTokenExpiresIn();
        return accessTokenExpiresIn.getAccessToken();
    }

    /**
     * 凭证和凭证有效时间类
     */
    @Data
    private static class AccessTokenExpiresIn {
        private String accessToken;
        // 过期时间
        private long expireTime;

        public AccessTokenExpiresIn(String accessToken, String expiresIn) {
            this.accessToken = accessToken;
            this.expireTime = System.currentTimeMillis() + Integer.parseInt(expiresIn) * 1000;
        }

        public boolean alreadyExpired() {
            return System.currentTimeMillis() > expireTime;
        }
    }

}

二、网页授权

(1) 关于网页授权

https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

  • 如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制获取用户基本信息,进而实现业务逻辑

网页授权流程有以下四步:

  • 引导用户进入授权页面同意授权,获取 code
  • 通过 code 换取网页授权 access_token(与之前的 access_token 不同)
  • 如果需要,开发者可以刷新网页授权 access_token,避免过期
  • 通过网页授权 access_token 和 openid 获取用户基本信息

① 引导用户进入授权页面同意授权,获取 code

在前端引导用户访问右边的地址:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

  • appid【必填】:公众号的唯一标识
  • redirect_uri【必填】:授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理(与 code 配合换取微信授权 access_token)
  • response_type【必填】:默认填写 code 即可
  • scope【必填】:应用授权作用域。snsapi_base (不弹出授权页面而直接跳转,只能获取用户的 openid);snsapi_userinfo (弹出授权页面,可通过 openid 拿到昵称、性别、所在地等。即使用户未关注公众号,只要用户授权,也能获取其信息
  • state【非必填】:重定向后会带上 state 参数,开发者可填写【a-zA-Z0-9】的参数值(最多128字节)
  • wechat_redirect【必填】:无论直接打开还是做页面302重定向,必须带此参数

在这里插入图片描述

② 通过 code 换取网页授权 access_token(与之前的 access_token 不同)

使用 ① 中获得到的 code 作为参数,发送给【https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code】获取 access_token、expires_in、refresh_token、openid、scope

三、模板消息

模板消息介绍:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html

模板消息运营规范:
https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Operation_Specifications.html

相关文章
|
3月前
|
小程序 Java 关系型数据库
基于Java微信小程序智能招聘平台设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序智能招聘平台设计和实现(源码+LW+调试文档+讲解等)
|
3月前
|
小程序 Java 关系型数据库
基于Java微信小程序同城家政服务系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序同城家政服务系统设计和实现(源码+LW+调试文档+讲解等)
|
3月前
|
小程序 Java 关系型数据库
基于Java微信小程序小说阅读系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序小说阅读系统设计和实现(源码+LW+调试文档+讲解等)
|
1月前
|
JavaScript 前端开发 Java
|
3月前
|
小程序 安全 Java
基于Java微信小程序民宿短租系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序民宿短租系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序民宿短租系统设计和实现(源码+LW+调试文档+讲解等)
|
3月前
|
小程序 JavaScript Java
基于Java微信小程序火锅店点餐系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序火锅店点餐系统设计和实现(源码+LW+调试文档+讲解等)
|
3月前
|
小程序 Java 关系型数据库
基于Java微信小程序自驾游拼团设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序自驾游拼团设计和实现(源码+LW+调试文档+讲解等)
|
3月前
|
小程序 JavaScript Java
基于Java微信小程序校园自助打印系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序校园自助打印系统设计和实现(源码+LW+调试文档+讲解等)
|
3月前
|
小程序 JavaScript Java
基于Java微信小程序火锅店点餐系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序火锅店点餐系统设计和实现(源码+LW+调试文档+讲解等)