2021-07-05

java 微信授权登录配置

官网地址:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

官网提供的四个步骤

  1. 第一步:用户同意授权,获取code
  2. 第二步:通过code换取网页授权access_token
  3. 第三步:刷新access_token(如果需要)
  4. 第四步:拉取用户信息(需scope为 snsapi_userinfo)
  5. 附:检验授权凭证(access_token)是否有效

一、获取code

  • 所需要的参数

image

  • 授权效果

image

  • 错误码的返回

image

二、获取access_token

第一步会获得一个微信返回的code,拿着这个CODE 还有APPID还有公钥往微信发送请求

 // 1.调用getHTMLAccessToken JSONObject htmlAccessToken = WeChatUtil.getHTMLAccessToken(code); // 2.获取用户授权的微信地址 public static final String GET_HTML_ACCESS_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; /** java 项目 www.fhadmin.cn  * 3.根据code获取access_token  * @param code  * @return access_token,open_id  */ public static JSONObject getHTMLAccessToken(String code) {  String replace =GET_HTML_ACCESS_TOKEN.replace("APPID", WeChatResources.APPID).replace("SECRET", WeChatResources.APPSECRET).replace("CODE", code);  log.info("请求url:{}",replace);  JSONObject jsonObject = HttpUtil.doGet(replace);  return jsonObject; } /**  * 4.发送请求的doGET方法  */  public static JSONObject doGet(String url) {  HttpClient httpClient = HttpClientBuilder.create().build();  HttpGet get = new HttpGet(url);  JSONObject jsonObject = null;  try {   HttpResponse response = httpClient.execute(get);   HttpEntity entity = response.getEntity();   if (null != entity) {    String result = EntityUtils.toString(entity);    jsonObject = JSONObject.fromObject(result);   }  } catch (IOException e) {   e.printStackTrace();  }  return jsonObject; }// 5.方法响应成功后获取access_token和openidObject access_token = htmlAccessToken.get("access_token");Object openid = htmlAccessToken.get("openid");

 

  • 参数说明

image

  • 返回参数说明

image

三、刷新access_token

  • 由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。

  • 获取第二步的refresh_token后,请求以下链接获取access_token:
    https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN

  • 请求方式同步骤二 用 HttpUtil.doGet(replace)

  • 参数说明

image

  • 返回参数说明

image

四、拉取用户信息

 //1.根据access_token,open_id获取用户信息 从而完成微信的授权登入 JSONObject userInfo = WeChatUtil.g......

原文转载:http://www.shaoqun.com/a/848229.html

跨境电商:https://www.ikjzd.com/

海带宝:https://www.ikjzd.com/w/1548

shirley:https://www.ikjzd.com/w/1684

55海淘网:https://www.ikjzd.com/w/1723


官网地址:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html官网提供的四个步骤第一步:用户同意授权,获取code第二步:通过code换取网页授权access_token第三步:刷新access_token(如果需要)第四步:拉取用户信息(需scope为snsap
黄劲:https://www.ikjzd.com/w/2426
紧急:美国严格执行ISF-5申报,申报不及时将面临高额罚金!:https://www.ikjzd.com/articles/21925
众多优质大平台可选择入驻!wish在全球电商排名为21?:https://www.ikjzd.com/articles/21927
6月1日起亚马逊要开始征收卖家税了吗?:https://www.ikjzd.com/articles/21929
做Dropshipping,发生退货怎么处理?:https://www.ikjzd.com/articles/21930
女人都喜欢被㖭哪里 喜欢让人㖭我下面:http://lady.shaoqun.com/a/247875.html
老头一边吃奶一边摸下面 被老头做的死去活来:http://lady.shaoqun.com/m/a/247872.html
婚外性导师 她趴着张开双腿让我进入(2/2):http://www.30bags.com/m/a/249567.html
在"多人运动"之后,你可能会得20种疾病,其中一些你负担不起...:http://lady.shaoqun.com/a/403738.html
新手做Shopee,迟迟不出单怎么办?:https://www.ikjzd.com/articles/146360
搞笑GIF图:今晚让语文老师给你上课,你愿意吗:http://lady.shaoqun.com/a/403739.html
幼儿园夏季有60个户外运动游戏和玩法,由幼儿园老师收集:http://lady.shaoqun.com/a/403740.html

No comments:

Post a Comment