一、获取apikey,appsecret与商户号
注册公众号、商户号
二、获取用户的OpenId
1.设置【授权回调页面域名】
官方解释:用户在网页授权页同意授权给公众号后,微信会将授权数据传给一个回调页面,回调页面需在此域名下,以确保安全可靠。回调页面域名不支持IP地址。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1cAAAAhCAIAAAC6M8bmAAAJX0lEQVR4nO2du46rOhSGz8vlXXgUmrxAunSR0tLSolS8AFKKNFvREYqQkGgi+RTYZi1fsCHenFz+T1PsmTGXrC2bz2vZzD9C8efPvwK8BmK4MQg4AAAAsJp/9L/wQH0dxHBjEHAAAABgNbDAlCCGG4OAAwAAAKuBBaYEMdwYBBwAAABYDSwwJYjhxiDgAAAAwGpggSlBDDcGAQcAAABWAwtMCWK4MQg4AAAAsBpYYEoQw41BwAEAAIDVwAJTghhuDAIOAAAArAYWmBLEcGMQcAAAAGA1sMCUIIYbg4ADAAAAq4EFpgQx3BgEHAAAAFgNLDAliOHGIOAAAADAamCBKUEMNwYBBwAAAFYDC0zJyzFsL8d9gOOljWqn2zaF6/DjpSUXnb7jrR2neyteDvjzdL7vDrNf5/4mxK1uA80O993hntVPIYQQQ37oKtf1qlK2zK/yJ7e6zeqnEEPuvzphyA/t6aG+e/QZv5A+P/ki7Q0efRb4RM5Pwe8htoE3JjwOced89FngHsStbnflMNcCfDLBMdA9XDXFfl804TMWjesCniO/gBXhbIo9f4x4wtMU+6LxXOB7A7qGa0eHrAUj2LWznhQLgAWmJIkFzvULrm9xB459kB1OTtNejqx/854dc+n/kyQWqIXMwaPP3L1r/kBtPKbb2YdM9mNfS/Vtl9u5vsqBnVA8T+f29LD9UtmYcUVjKLEUk92Y81fXLuY+nXELWOC12527fEbZ5Z17ZDooxOCziR6eAhZYNPpk1phKRtJfY3oszKUJXHpHg2qEL/S0+zmqkoyN9hR9NEI+xo5jJjtwObDAlKTKBcquwYysvRz3xcUz1s31p6aQ89qiMS2wKawp3m/mAmUXYhr0PJ3vee2xwBlDEoLmvW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X1+Ayqusj5UVq61GxOHDpW3ssT5ULdwLNfyALAXrQ5Vl+li0WLMeHNmKC1YbTfm61rnDk2knDvUyheHAsBeNDlUXlumv7voz01iFl+rEq+OP38t76g5NL7FBYcCwF6sO1Re86T/ja+1imqzW2rTizVntV/LOyQjnXCY721uL+fFofrCHBwKAN2sODQas+XVgLN5CVftSVJ1lrs11OfQk3nVatGh+rt7+1ji0PbXeAHAb0vP86G5Q1VSorx4sS9YZ9kg0To0fk+p9sy81W7xFWZ6H8ylUHPoptfJAsBvy24O1WeD4l/dK2RVsu0pbKU4wdqyj5uKJQ4FgBzWHAEA6AeHAgD0g0MBAPrBoQAA/eBQAIB+cCgAQD84FACgHxwKANAPDgUA6AeHAgD0g0MBAPrBoQAA/eBQAIB+cCgAQD84FACgHxwKANAPDgUA6AeHAgD0g0MBAPrBoQAA/eBQAIB+cCgAQD84FACgHxwKANAPDgUA6AeHAgD08//Pj68h7W8cigAAAABJRU5ErkJggg==" alt="" />
2.用户同意授权
我是把这个url写在微信菜单下的,当进入这个页面的时候就让用户同意。注意:好像是静默授权的,用户不知道
1.url:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=url&response_type=code&scope=snsapi_userinfo&state=park#wechat_redirect
参数:appid:公众号的唯一标识
redirect_uri:重定向的url,就是授权后要跳转的页面
scope:应用授权作用域
snsapi_base:不弹出授权页面,直接跳转,只能获取用户openid
snsapi_userinfo:弹出授权页面,可通过openid拿到昵称、性别、所在地
state:重定向后带的参数
2.用户同意后会产生一个code,只有5分钟时间的有效期。
String code = request.getParameter("code")
3.code换openId
/**
* 常量类
* @author rory.wu
*
*/
public class Constants {
// 第三方用户唯一凭证
public static String appid = "";
// 第三方用户唯一凭证密钥
public static String appsecret = "";
//商户ID
public static String mch_id="";
//获取openId
public static String oauth2_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
}
/**
* 通用工具类
* @author rory.wu
* @version 1.0
* @since 2015年08月05日
*/
public class CommonUtil {
private static Logger log = Logger.getLogger(CommonUtil.class);
public static JSONObject httpsRequestToJsonObject(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
try {
StringBuffer buffer = httpsRequest(requestUrl, requestMethod, outputStr);
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
log.error("连接超时:"+ce.getMessage());
} catch (Exception e) {
log.error("https请求异常:"+e.getMessage());
}
return jsonObject;
}
private static StringBuffer httpsRequest(String requestUrl, String requestMethod, String output)
throws NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException, MalformedURLException,
IOException, ProtocolException, UnsupportedEncodingException {
URL url = new URL(requestUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod(requestMethod);
if (null != output) {
OutputStream outputStream = connection.getOutputStream();
outputStream.write(output.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
connection.disconnect();
return buffer;
} }
/**
* 获取用户的openId,并放入session
* @param code 微信返回的code
*/
private void setOpenId(String code) {
session.put("code", code);
String oauth2_url = Constants.oauth2_url.replace("APPID", Constants.appid).replace("SECRET", Constants.appsecret).replace("CODE", String.valueOf(session.get("code")));
log.info("oauth2_url:"+oauth2_url);
JSONObject jsonObject = CommonUtil.httpsRequestToJsonObject(oauth2_url, "POST", null);
log.info("jsonObject:"+jsonObject);
Object errorCode = jsonObject.get("errcode");
if(errorCode != null) {
log.info("code不合法");
}else{
String openId = jsonObject.getString("openid");
log.info("openId:"+openId);
session.put("openId", openId);
}
}
oauth2_url返回的格式是:
{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID", "scope":"SCOPE",
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
Code无效时:
{
"errcode":40029
,"errmsg":"invalid code"
}
微信公众号网页授权获取用户openid
最近一个项目是在微信公众号内二次开发,涉及到微信公众号支付,根据文档要求想要支付就必须要获取到用户的openid. 这是微信官方文档https://mp.weixin.qq.com/wiki?t=re ...
微信网页授权-公众号支付(获取openid、用户信息等)
名词解释: openid 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID 业务功能描述:实现H5页面可以在微信浏览器里面进行微信支付,所以需要 ...
thinkphp.2 thinkphp5微信支付 微信公众号支付 thinkphp 微信扫码支付 thinkphp 微信企业付款5
前面已经跑通了微信支付的流程,接下来吧微信支付和微信企业付款接入到thinkphp中,版本是3.2 把微信支付类.企业付款类整合到一起放到第三方类库,这里我把微信支付帮助类和企业付款类放到同一个文件了 ...
微信公众号开发系列-获取微信OpenID
在微信开发时候在做消息接口交互的时候须要使用带微信加密ID(OpenId),下面讲讲述2中类型方式获取微信OpenID.接收事件推送方式和网页授权获取用户基本信息方式获取. 1.通过接收被动消息方式获 ...
微信公众号支付JSAPI网页,total_fee错误不正确,header重定向参数丢失,无法获取订单号和金额解决
微信公众号支付官方demo错误, 公众号支付只能用在微信里,也就是微信内部浏览器. 1.到WxPayHubHelper.php文件 JsApi_pub()类下createOauthUrlForCode ...
使用开源库MAGICODES.WECHAT.SDK进行微信公众号支付开发
概要 博客使用Word发博,发布后,排版会出现很多问题,敬请谅解.可加群获取原始文档. 本篇主要讲解微信支付的开发流程,相关业务基于MAGICODES.WECHAT.SDK实现.通过本篇教程,您可以很 ...
微信公众号支付流程(Node实现)
前言 花费了一天时间,调通了微信公众号支付.作下记录,方便以后再次填坑.先声明,微信公众号支付,不同于微信H5支付,这点在本文结束时再详细说明. 微信配置 设置测试目录 在微信公众平台设置,栏目见下图 ...
微信公众号支付开发全过程 --JAVA
按照惯例,开头总得写点感想 ------------------------------------------------------------------ 业务流程 这个微信官网说的还是很详细的 ...
到处是坑的微信公众号支付开发(java)
之前公司项目开发中支付是用阿里的支付做的,那叫一个简单,随意:悲催的是,现在公司开发了微信公众号,所以我步入了全是坑的微信支付开发中... ------------------------------ ...
微信公众号支付|微信H5支付|微信扫码支付|小程序支付|APP微信支付解决方案总结
最近负责的一些项目开发,都用到了微信支付(微信公众号支付.微信H5支付.微信扫码支付.APP微信支付).在开发的过程中,在调试支付的过程中,或多或少都遇到了一些问题,今天总结下,分享,留存. 先说注意 ...
随机推荐
Android中 int 和 String 互相转换的多种方法
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...
Genome2D编译方法
Genome2D是一个高效的2D引擎,现在支持Flash(stage3d)和HTML5,因为只有作者一个人在维护,就没开源代码. 最近和作者沟通了下,已经开源啦. 作者划分了几个模块,编译起来不是特别 ...
2013年江西理工大学C语言程序设计竞赛(初级组)
ACM ICPC WORLD FINAL 解法:排序大家都知道,去重的话,初学者用数组就好了 #include #include us ...
win7中protel99添加元件库
protel打开时默认添加Miscellaneous Devices.lib,点击ADD/REMOVE按钮添加所需要的库时,弹出“文件无法识别”. 点击面板右下方“FIND”按钮,弹出如下对话框时不要 ...
程序不稳定是因为C++基础不扎实
最近开发的程序,逻辑上都实现了,但是感觉运行不稳定,程序时不时崩溃(不是逻辑运行不正确),至少找出2个错误: 情况1:char* szRemoteReal = new char[MAX_LENGTH] ...
C++ primer札记10-继承
包.继承,多态性C++的三个基本概念,在这里,我们重点总结继承的东西 1 类派生列表 类派生列表中指定一个派生类继承基类,来自列表与一个或多个基类如: class B : public A1,prot ...
[Spark内核] 第37课:Task执行内幕与结果处理解密
本课主题 Task执行内幕与结果处理解密 引言 这一章我们主要关心的是 Task 是怎样被计算的以及结果是怎么被处理的 了解 Task 是怎样被计算的以及结果是怎么被处理的 Task 执行原理流程图 ...
内置函数 -- bytes -- 字节码与字符串相互转换
说明: 1. 返回值为一个新的不可修改字节数组,每个数字元素都必须在0 - 255范围内,是bytearray函数的具有相同的行为,差别仅仅是返回的字节数组不可修改. 2. 当3个参数都不传的时候,返 ...
git工具——版本的创建与回退
1.创建一个版本库 进入要管理的文件路径:cd f:/ZK/Opencv3.4.2-YOLOv3 输入命令: git init 2.版本创建与回退 在文件目录下创建一个文件code.txt: vi c ...
用户ID的代码生成
public class Uid { private static final String machineIdString = Integer.toHexString(new Object().ha ...