一、获取apikey,appsecret与商户号
注册公众号、商户号
二、获取用户的openid
1.设置【授权回调页面域名】
官方解释:用户在网页授权页同意授权给公众号后,微信会将授权数据传给一个回调页面,回调页面需在此域名下,以确保安全可靠。回调页面域名不支持ip地址。
2.用户同意授权
我是把这个url写在微信菜单下的,当进入这个页面的时候就让用户同意。注意:好像是静默授权的,用户不知道
1.url:
https://open.weixin.qq.com/connect/oauth/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,只有分钟时间的有效期。
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 oauth_url = "https://api.weixin.qq.com/sns/oauth/access_token?appid=appid&secret=secret&code=code&grant_type=authorization_code";
}
/**
* 通用工具类
* @author rory.wu
* @version .
* @since 年月日
*/
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-"));
outputstream.close();
}
// 从输入流读取返回内容
inputstream inputstream = connection.getinputstream();
inputstreamreader inputstreamreader = new inputstreamreader(inputstream, "utf-");
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 oauth_url = constants.oauth_url.replace("appid", constants.appid).replace("secret", constants.appsecret).replace("code", string.valueof(session.get("code")));
log.info("oauth_url:"+oauth_url);
jsonobject jsonobject = commonutil.httpsrequesttojsonobject(oauth_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);
}
}
oauth_url返回的格式是:
{
"access_token":"access_token",
"expires_in":,
"refresh_token":"refresh_token",
"openid":"openid", "scope":"scope",
"unionid": "o_bmasdasdsad_sgvthmzopfl"
}
code无效时:
{
"errcode":
,"errmsg":"invalid code"
}
以上内容就是脚本之家的小编给大家分享的微信公众号支付(一)如何获取用户openid,希望大家喜欢。
更多微信公众号支付(一)如何获取用户openid。