微信小程序登录java后台接口首先看一下微信小程序的开发文档:微信小程序开发文档步骤:小程序端向微信接口服务发送请求——wx.login();获取到登录临时凭证code小程序端拿着获取到的code向后台(这里是java服务端),使用wx.request()向自己的服务器发送请求(接口服务器自己定义)后台服务器拿着小程序端传过来的code,以及自己的APPID,secretKey向微信方发送HttpGet请求后台服务器获取到微信方返回回来的openId,session_key,然后加上自己本地的登录状态(本地自己定义),发送给小程序端小程序端接收到后台传输过来的登录状态,保存到storage中以供以后使用。具体实现步骤小程序端随便弄一个按钮,绑定下面的方法 //与后端通信 bindtest: function (){ wx.login({ success:res=>{ let _code=res.code; //获取到code之后再发送给后端 wx.request({ url: ‘http://localhost:8080/login’, data:{ code:_code, }, method:’POST’, header: { ‘content-type’: ‘application/x-www-form-urlencoded’ // 默认是json }, success: function (res) { console.log(res.data); }, fail: function (res) { console.log(“…..fail…..”); } }) } }) }java服务器端package com.kylin.wxtest.controller;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.kylin.wxtest.bean.Login;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;@RestControllerpublic class UserController { private static final long serialVersionUID=1L; private static final String APPID = “wxb88xxxxxxxx46140e”; private static final String SECRET = “19fa40c6xxxxxxxx6ae971267”; private String code; public String getCode() { return code; } public void setCode(String code) { this.code = code; } @RequestMapping(value = “/login”) public String login(String code){ System.out.println(code); System.out.println(“————————————“); //微信那边的接口,grant_type=authorization_code是固定的 String url=”https://api.weixin.qq.com/sns/jscode2session?appid=”+APPID+ “&secret=”+SECRET+”&js_code=”+ code +”&grant_type=authorization_code”; //发送请求给微信后端 CloseableHttpClient httpClient= HttpClients.createDefault(); HttpGet httpGet=new HttpGet(url); InputStream inputStream=null; CloseableHttpResponse httpResponse=null; StringBuilder result=new StringBuilder(); try { httpResponse=httpClient.execute(httpGet); HttpEntity entity=httpResponse.getEntity(); inputStream=entity.getContent(); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream)); String line=””; while ((line=bufferedReader.readLine())!=null){ System.out.println(line); //这里需要使用fastjson来提取一下内容 JSONObject jsonObject= JSON.parseObject(line); Login login=new Login(); login.setOpenid(jsonObject.getString(“openid”)); login.setSession_key(jsonObject.getString(“session_key”)); result.append(login.getOpenid()+”hello_world”+login.getSession_key()); System.out.println(result.toString()); } } catch (IOException e) { e.printStackTrace(); } return result.toString(); }}总结这样小程序端就可以获取到openId以及session_key;相当于授权成功!
微信小程序登录Java后台接口
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信小程序登录Java后台接口
相关推荐
店铺被隐形降权东乡县E企盈小程序?采取哪些方法可以将影响降至最低? – E企盈小程序
微信小程序|核桃编程X支付宝小程序“小程序编程马拉松”决赛本周克东县E企盈小程序开战
大城县E企盈小程序
小程序快递华阴市E企盈小程序查询
微信小程序生成二龙泉市E企盈小程序维码并通过fastdfs保存
微慈利县E企盈小程序信公众号开发之生成并扫描带参数的二维码
小程序类似发布朋友圈的功能(图片上传和昌图县E企盈小程序修改)
对于小程序拦含山县E企盈小程序截器http请求
微信小程序云开发:使用excelexpo排浦镇E企盈小程序rt导出excel
微信小大洼县E企盈小程序程序给数组里的某一项赋值
微信小翼城县E企盈小程序程序商城系列之购物车
微信小程序之重新调起授邵东县E企盈小程序权
最新评论
不错的小程序案例
优秀的团队,不错的服务!
讲的很好
主播长的帅气
好系统好服务
优秀的团队
好服务,值得信赖
不错的服务