首先看一下微信小程序的開發文檔:
微信小程序開發文檔
- 小程序端向微信接口服務發送請求——wx.login();獲取到登錄臨時憑證code
- 小程序端拿著獲取到的code向后臺(這里是java服務端),使用wx.request()向自己的服務器發送請求(接口服務器自己定義)
- 后臺服務器拿著小程序端傳過來的code,以及自己的APPID,secretKey向微信方發送HttpGet請求
- 后臺服務器獲取到微信方返回回來的openId,session_key,然后加上自己本地的登錄狀態(本地自己定義),發送給小程序端
- 小程序端接收到后臺傳輸過來的登錄狀態,保存到storage中以供以后使用。愛掏網 - it200.com
小程序端
隨便弄一個按鈕,綁定下面的方法
//與后端通信
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;
@RestController
public 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;相當于授權成功!