激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 微信公眾號支付(一)如何獲取用戶openId

微信公眾號支付(一)如何獲取用戶openId

2020-01-04 17:02mdxy-dxy JAVA教程

本篇文章給大家介紹微信公眾號支付如何獲取用戶openid,需要授權回調頁面域名等一系列途徑實現此功能,需要的朋友可以參考下

一、獲取apikey,appsecret與商戶號

  注冊公眾號、商戶號

二、獲取用戶的OpenId

  1.設置【授權回調頁面域名】

    官方解釋:用戶在網頁授權頁同意授權給公眾號后,微信會將授權數據傳給一個回調頁面,回調頁面需在此域名下,以確保安全可靠。回調頁面域名不支持IP地址。

微信公眾號支付(一)如何獲取用戶openId

  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,只有分鐘時間的有效期。

?
1
String code = request.getParameter("code")

    3.code換openId

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 常量類
 * @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";
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
 * 通用工具類
 * @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,希望大家喜歡。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人在线观看网站 | 日本aaa一级片 | 91麻豆精品国产91久久久无需广告 | 特级毛片免费 | 免费激情网站 | 99精品在线观看 | 日韩精品中文字幕在线播放 | 亚洲免费片 | 午夜a狂野欧美一区二区 | 鲁一鲁一鲁一鲁一曰综合网 | 国产日韩大片 | 国产精品麻豆一区二区三区 | a级黄色片视频 | 久久99精品久久久久久园产越南 | 亚洲精品午夜电影 | 欧美成在人线a免费 | 欧美日韩大片在线观看 | 性欧美性欧美 | 黄色网址入口 | 国产成人精品一区二区仙踪林 | 欧美成人免费香蕉 | 国产亲子伦在线观看 | 毛片大全在线观看 | 欧美精品免费一区二区三区 | 欧美日韩一 | 欧美精品成人 | 美国一级毛片片aa久久综合 | 91精彩在线 | 97porn | 日本精品免费观看 | 精品国产视频一区二区三区 | xxxxxx中国 | 久草在线高清 | 毛片免费视频观看 | 久久精品视频69 | 色999国产| 黑人一级片视频 | 国产精品.com | 九九热精品免费视频 | 天堂热 | 国内精品视频饥渴少妇在线播放 |