本文主要探讨微信内置网页开发中如何获取用户的openid,姓名,性别,地点等基本信息。
下面我将从零开始讲解微信网页开发中如何获取用户基本信息的。这里我们用微信免费的测试账号。
1,首先我们去注册一个微信公众号,那个不要钱的就可以,如果后续要商用的话,肯定还得注册收费的那些。怎么
注册之类的我就不细说了。
2,登录进去,点最下面的开发->开发者工具->公众平台测试账号,接下来就开始配置我们服务器了。
3,进去之后会有一个接入配置,下面直接给源码
<%@page import="java.util.Date"%>
<%@page import="org.dom4j.Element"%>
<%@page import="org.dom4j.DocumentHelper"%>
<%@page import="org.dom4j.Document"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.Reader"%>
<%@page import="java.security.MessageDigest"%>
<%@page import="java.util.Arrays"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
final String TOKEN="weixin";
final HttpServletRequest final_request=request;
final HttpServletResponse final_response=response;
%>
<%
class WeiXinHandler{
public void valid(){
String echostr=final_request.getParameter("echostr");
if(null==echostr||echostr.isEmpty()){
responseMsg();
}else{
if(this.checkSignature()){
this.print(echostr);
}else{
this.print("error");
}
}
}
public void responseMsg(){
String postStr=null;
try{
postStr=this.readStreamParameter(final_request.getInputStream());
}catch(Exception e){
e.printStackTrace();
}
//System.out.println(postStr);
if (null!=postStr&&!postStr.isEmpty()){
Document document=null;
try{
document = DocumentHelper.parseText(postStr);
}catch(Exception e){
e.printStackTrace();
}
if(null==document){
this.print("");
return;
}
Element root=document.getRootElement();
String fromUsername = root.elementText("FromUserName");
String toUsername = root.elementText("ToUserName");
String keyword = root.elementTextTrim("Content");
String time = new Date().getTime()+"";
String textTpl = "<xml>"+
"<ToUserName><![CDATA[%1$s]]></ToUserName>"+
"<FromUserName><![CDATA[%2$s]]></FromUserName>"+
"<CreateTime>%3$s</CreateTime>"+
"<MsgType><![CDATA[%4$s]]></MsgType>"+
"<Content><![CDATA[%5$s]]></Content>"+
"<FuncFlag>0</FuncFlag>"+
"</xml>";
if(null!=keyword&&!keyword.equals(""))
{
String msgType = "text";
String contentStr = "Welcome to wechat world!";
String resultStr = textTpl.format(textTpl, fromUsername, toUsername, time, msgType, contentStr);
this.print(resultStr);
}else{
this.print("Input something...");
}
}else {
this.print("");
}
}
public boolean checkSignature(){
String signature = final_request.getParameter("signature");
String timestamp = final_request.getParameter("timestamp");
String nonce = final_request.getParameter("nonce");
String token=TOKEN;
String[] tmpArr={token,timestamp,nonce};
Arrays.sort(tmpArr);
String tmpStr=this.ArrayToString(tmpArr);
tmpStr=this.SHA1Encode(tmpStr);
if(tmpStr.equalsIgnoreCase(signature)){
return true;
}else{
return false;
}
}
public void print(String content){
try{
final_response.getWriter().print(content);
final_response.getWriter().flush();
final_response.getWriter().close();
}catch(Exception e){
}
}
//����ת�ַ���
public String ArrayToString(String [] arr){
StringBuffer bf = new StringBuffer();
for(int i = 0; i < arr.length; i++){
bf.append(arr[i]);
}
return bf.toString();
}
//sha1
public String SHA1Encode(String sourceString) {
String resultString = null;
try {
resultString = new String(sourceString);
MessageDigest md = MessageDigest.getInstance("SHA-1");
resultString = byte2hexString(md.digest(resultString.getBytes()));
} catch (Exception ex) {
}
return resultString;
}
public final String byte2hexString(byte[] bytes) {
StringBuffer buf = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
if (((int) bytes[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString((int) bytes[i] & 0xff, 16));
}
return buf.toString().toUpperCase();
}
public String readStreamParameter(ServletInputStream in){
StringBuilder buffer = new StringBuilder();
BufferedReader reader=null;
try{
reader = new BufferedReader(new InputStreamReader(in));
String line=null;
while((line = reader.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(null!=reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buffer.toString();
}
}
%>
<%
WeiXinHandler handler=new WeiXinHandler();
handler.valid();
%>
直接运行上面的JSP文件,用tomcat就可以,端口用8080,用ngrok外网映射,然后把访问该jsp文件的外网域名填入
进去,token就填weixin。
此时微信基本配置成功。
接下来我们按照官网一步步来开发,
第一步:用户同意授权,获取code
在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect 若提示“该链接无法访问”,请检查参数是否填写错误,是否拥有scope参数对应的授权作用域权限。
尤其注意:由于授权操作安全等级较高,所以在发起授权请求时,微信会对授权链接做正则强匹配校验,如果链接的参数顺序不对,授权页面将无法正常访问
参考链接(请在微信客户端中打开此链接体验):
scope为snsapi_base
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect
scope为snsapi_userinfo
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
尤其注意:跳转回调redirect_uri,应当使用https链接来确保授权code的安全性。
对于第一步,就是打开一个链接,下面给出一个html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http://23a57e9f.ngrok.io/test1/weixin_token/hello.jsp&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect">1</a>
</body>
</html>
第二步
获取code后,请求以下链接获取access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
即用第一步转到的url后面带的code来请求另一个地址,此时会返回一个json,这部份代码和第四部在一起的,会在最后给出,这部就可以得到我们的openid了。
第三步和第二步一样,就是再访问另一个url,这部可以忽略。
第四步
第四步:拉取用户信息(需scope为 snsapi_userinfo)
如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。
请求方法
http:GET(请使用https协议) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
参数说明
参数 | 描述 |
---|---|
access_token | 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同 |
openid | 用户的唯一标识 |
lang | 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语 |
用第二步或第三步获得的access_token请求另一个url得到其他个人信息。下面给出第二步和第四步的源码