First, obtain Apikey,appsecret and merchant number
Registered public number, merchant number
Second, access to the user's OpenID
1. Set "Authorization callback page domain name"
Official explanation: After the user agrees to authorize the public number on the page authorization page, the micro-letter will pass the authorization data to a callback page, the callback page must be under this domain name, to ensure safe and reliable. The callback page domain name does not support IP addresses.
2. User agrees to authorize
I write this URL under the micro-letter menu, when entering this page let the user agree. Note: It seems to be a silent authorization, the user does not know
1.url:
https://open.weixin.qq.com/connect/oauth/authorize?appid=appid&redirect_uri=url&response_type= Code&scope=snsapi_userinfo&state=park
Parameters: AppID: Unique identification of the public number
Redirect_uri: Redirected URL, which is the page to jump after authorization
Scope: Apply an authorization scope
Snsapi_base: Does not eject the authorization page, the direct jump, only obtains the user OpenID
Snsapi_userinfo: Eject authorization page, can get nickname, sex, location by OpenID
State: redirected back with parameters
2. User consent will produce a code, only minutes of the validity period.
String code = request.getparameter ("code")
3.code Change OpenID
/**
* Constant class
* @author RORY.WU
* * *
/Public
class Constants {
//Third party user unique voucher public
static String AppID = "";
Third party user unique voucher key public
static String Appsecret = "";
Merchant ID public
static String mch_id= "";
Get 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 ";
}
/** * General Purpose Tool Class * @author Rory.wu * @version.
* @since Month/day * * public class Commonutil {private static Logger log = Logger.getlogger (Commonutil.class); public static Jsonobject Httpsrequesttojsonobject (String requesturl, String Requestmethod, String outputstr) {Jsonobjec
T jsonobject = null;
try {stringbuffer buffer = httpsrequest (Requesturl, Requestmethod, OUTPUTSTR);
Jsonobject = Jsonobject.fromobject (buffer.tostring ());
catch (connectexception CE) {log.error ("Connection timeout:" +ce.getmessage ());
catch (Exception e) {log.error ("https request exception:" +e.getmessage ());
return jsonobject; private static StringBuffer Httpsrequest (String requesturl, String Requestmethod, string output) throws Nosuchalgor Ithmexception, 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 ();
///reads the return content from the input stream 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; }/** * Gets the user's OpenID and puts it into session * @param code micro-Letter return code */private void Setopenid (String code) {session.put ("C
Ode ", 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 not valid");
}else{String OpenID = jsonobject.getstring ("OpenID");
Log.info ("OpenId:" +openid);
Session.put ("OpenID", OpenID); The format returned by Oauth_url is: {"Access_token": "Access_token", "expires_in":, "Refresh_token": "Refresh_token", "open ID ": OPENID", "Scope": "Scope", "Unionid": "O_BMASDASDSAD_SGVTHMZOPFL"} Code is invalid: {"Errcode":, "errmsg": "Inval ID Code "}
The above is a small part of the cloud-dwelling community to share the micro-letter public number to pay (i) How to obtain user OpenID, I hope you like.