一、獲取apikey,appsecret與商戶(hù)號(hào)
注冊(cè)公眾號(hào)、商戶(hù)號(hào)
二、獲取用戶(hù)的OpenId
1.設(shè)置【授權(quán)回調(diào)頁(yè)面域名】
官方解釋?zhuān)河脩?hù)在網(wǎng)頁(yè)授權(quán)頁(yè)同意授權(quán)給公眾號(hào)后,微信會(huì)將授權(quán)數(shù)據(jù)傳給一個(gè)回調(diào)頁(yè)面,回調(diào)頁(yè)面需在此域名下,以確保安全可靠?;卣{(diào)頁(yè)面域名不支持IP地址。
2.用戶(hù)同意授權(quán)
我是把這個(gè)url寫(xiě)在微信菜單下的,當(dāng)進(jìn)入這個(gè)頁(yè)面的時(shí)候就讓用戶(hù)同意。注意:好像是靜默授權(quán)的,用戶(hù)不知道
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
參數(shù):appid:公眾號(hào)的唯一標(biāo)識(shí)
redirect_uri:重定向的url,就是授權(quán)后要跳轉(zhuǎn)的頁(yè)面
scope:應(yīng)用授權(quán)作用域
snsapi_base:不彈出授權(quán)頁(yè)面,直接跳轉(zhuǎn),只能獲取用戶(hù)openid
snsapi_userinfo:彈出授權(quán)頁(yè)面,可通過(guò)openid拿到昵稱(chēng)、性別、所在地
state:重定向后帶的參數(shù)
2.用戶(hù)同意后會(huì)產(chǎn)生一個(gè)code,只有分鐘時(shí)間的有效期。
String code = request.getParameter("code")
3.code換openId
/** * 常量類(lèi) * @author rory.wu * */ public class Constants { // 第三方用戶(hù)唯一憑證 public static String appid = ""; // 第三方用戶(hù)唯一憑證密鑰 public static String appsecret = ""; //商戶(hù)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"; }
/** * 通用工具類(lèi) * @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("連接超時(shí):"+ce.getMessage()); } catch (Exception e) { log.error("https請(qǐng)求異常:"+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(); } // 從輸入流讀取返回內(nèi)容 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; } } /** * 獲取用戶(hù)的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無(wú)效時(shí): { "errcode": ,"errmsg":"invalid code" }
? ?
以上內(nèi)容就是腳本之家的小編給大家分享的微信公眾號(hào)支付(一)如何獲取用戶(hù)openId,希望大家喜歡。
更多微信公眾號(hào)支付(一)如何獲取用戶(hù)openId相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

熱AI工具

Undress AI Tool
免費(fèi)脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門(mén)文章

熱工具

記事本++7.3.1
好用且免費(fèi)的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開(kāi)發(fā)環(huán)境

Dreamweaver CS6
視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版
神級(jí)代碼編輯軟件(SublimeText3)