700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 微信小程序获取openid sessionKey

微信小程序获取openid sessionKey

时间:2022-09-14 04:34:37

相关推荐

微信小程序获取openid sessionKey

按照微信开放文档,服务端拿到小程序发送的code,去微信服务器换取openid

获取accessToken

package com.mon.utils;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import java.util.HashMap;import java.util.Map;/*** @Description TODO* @Author Yu* @Date -11-26*/public class WxUtils {public static JSONObject getSessionKeyOrOpenId(String code) {String requestUrl = "https://api./sns/jscode2session";Map<String, String> requestUrlParam = new HashMap<>();//小程序appIdrequestUrlParam.put("appid", "APPID");//小程序secretrequestUrlParam.put("secret", "SECRET");//小程序端返回的coderequestUrlParam.put("js_code", code);//默认参数requestUrlParam.put("grant_type", "authorization_code");//发送post请求读取调用微信接口获取openid用户唯一标识JSONObject jsonObject = JSON.parseObject(HttpClientUtils.doGet(requestUrl, requestUrlParam));return jsonObject;}public static JSONObject getAccessToken(){String requestUrl = "https://api./cgi-bin/token";Map<String, String> requestUrlParam = new HashMap<>();//小程序appIdrequestUrlParam.put("appid", "APPID");//小程序secretrequestUrlParam.put("secret", "SECRET");//默认参数requestUrlParam.put("grant_type", "client_credential");JSONObject jsonObject = JSON.parseObject(HttpClientUtils.doGet(requestUrl, requestUrlParam));return jsonObject;}}

HttpClientUtils见HttpClientUtils工具类

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。