首頁 微信小程式 微信開發 微信公眾號支付(一)如何取得用戶openId

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

Feb 04, 2017 am 11:36 AM

一、取得apikey,appsecret與商家號碼

  註冊公眾號、商戶號碼

二、取得使用者的OpenId

】網頁網頁授權頁同意授權給公眾號後,微信會將授權資料傳給一個回呼頁面,回呼頁面需在此網域下,以確保安全可靠。回調頁面網域不支援IP位址。


  2.用戶同意授權

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

    我是把這個url寫在微信菜單下的,當進入這個頁面的時候就讓用戶同意。註:好像是靜默授權的,使用者不知道


    1.url:

https://open.weixin.qq.com/connect/oauth/authorize?appid=appid&redirect_uri=url&responas_ffidmob;

    參數:appid:公眾號的唯一標識

       redirect_uri:重定向的url,就是授權後要跳轉的頁面


       scope:應用授權作用域


          snsapi_base:不彈出授權頁面,直接跳轉,只能取得使用者openid


          snsapi_userinfo:彈出授權頁面,可透過openid拿到暱稱、性別、導向🎀    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相關文章請追蹤PHP中文網!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)