app微信登录/小程序登录后台操作流程登录(java)

一、在微信开放平台创建移动应用


点击创建移动应用

二、拿到微信登录权限

三、app或小程序授权

1. app在拿到用户同意授权的时候,会传来一个code码:

    后台接受到app端传来的code码,远程调用接口:https://api.weixin.qq.com/sns/oauth2/access_token;请求接口为GET请求;参数为:appid,secret,code,grant_type
具体请求方式可参考:

public static String net(String strUrl, Map<String, Object> params, String method) throws Exception {
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String rs = null;
        try {
            StringBuffer sb = new StringBuffer();
            if (method == null || method.equals("GET")) {
                strUrl = strUrl + "?" + urlencode(params);
            }
            URL url = new URL(strUrl);
            conn = (HttpURLConnection) url.openConnection();
            if (method == null || method.equals("GET")) {
                conn.setRequestMethod("GET");
            } else {
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
            }
            conn.setRequestProperty("User-agent", userAgent);
            conn.setUseCaches(false);
            conn.setConnectTimeout(DEF_CONN_TIMEOUT);
            conn.setReadTimeout(DEF_READ_TIMEOUT);
            conn.setInstanceFollowRedirects(false);
            conn.connect();
            if (params != null && method.equals("POST")) {
                try {
                    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
                    out.writeBytes(urlencode(params));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            InputStream is = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sb.append(strRead);
            }
            rs = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return rs;
    }

调用方法参考:

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("appid", WeChatProperties.APPID);
        params.put("secret", WeChatProperties.SECRET);
        params.put("code", code);
        params.put("grant_type", WeChatProperties.GRANT_TYPE);
        Map<String, String> resultMap = (Map<String, String>) JSON
                .parse(Certification.net("https://api.weixin.qq.com/sns/oauth2/access_token", params, "GET"));

获取到返回值:

{
    "access_token": "",
    "refresh_token": "",
    "unionid": "",
    "openid": "",
    "scope": "",
    "expires_in": 7200
}

在使用spring boot时,也可以直接使用RestTemplate来进行调用,示例为:

Map<?, ?> map = new ObjectMapper()
                .readValue(
                        restTemplate
                                .getForEntity(MiniappProperties.login_url + "?appid=" + MiniappProperties.appid
                                    + "&secret=" + MiniappProperties.secret + "&grant_type="
                                    + MiniappProperties.grant_type + "&js_code=" + code, String.class)
                                .getBody(),
                        Map.class);

来直接调用。
    openid为在此应用下,此种登录方法的微信号的标识;可在数据库记录此字段,判断是否为同一微信号登录;unionid为在此应用下微信号的标识,可用来标识不同登录方式采用的是同一个微信号;

2. 小程序在拿到用户同意授权的时候,会传来一个code码:

    后台接受到app端传来的code码,远程调用接口:https://api.weixin.qq.com/sns/jscode2session;请求接口为GET请求;参数为:appid,secret,js_code,grant_type
除了参数不同,调用接口不同,其他使用方法与app登录相同
获取返回值:

{
    "openid": "",
    "session_key": "",
    "expires_in": 7200
}

openid为在此应用下,此种登录方法的微信号的标识;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • 注意:代码自己动手写,不要复制! GitHub 一、接入微信第三方登录准备工作。 移动应用微信登录是基于OAuth...
    大冲哥阅读 15,151评论 0 7
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,814评论 25 708
  • 响应式布局的原理就是通过@media这个标签(媒体查询),来对页面窗口不同尺寸时候应用不同的样式。 @medi...
    山间的树阅读 227评论 0 0
  • 相信很多人和我一样,从小到大听过的最多的一句励志的话便是:人生的道路不是平坦的,是坎坷的!是啊!无论大事小事,总...
    南多木阅读 292评论 0 0