Jsapi-java initial access, letter jsapi-java access
[Public account for developers]
Contact should be earlier than most people,
I remember that it would open a number, and most people do not know anything,
The rapid development is really powerful.
Many secondary developers develop public accounts,
But all are stuck on the "authentication,
Individuals cannot authenticate.
[Send a link to a Friend]
I encountered another problem today. I want to create a custom link to send to my friends,
This simple feature took an afternoon and finally found that authentication was required for sharing,
If you do not have any problems, make a record to help those who have the same problems.
[Java code]
As for the api or something, it is difficult, but it is not very difficult. It directly encapsulates a part of the Code:
Package com. uikoo9.util. external; import java. util. arrays; import java. util. hashMap; import java. util. map; import java. util. UUID; import javax. servlet. http. httpServletRequest; import org. json. JSONObject; import com. uikoo9.util. core. data. QStringUtil; import com. uikoo9.util. core. file. QPropertiesUtil; import com. uikoo9.util. core. http. QHttpClientUtil; import com. uikoo9.util. function. QCacheUtil; import com. uikoo9. Util. function. QEncodeUtil; import com. uikoo9.util. function. QThreadUtil;/*** tool class ** 1. verify validity <br> * 2. obtain the accesstoken <br> * 3. get jsticket <br> * 4. execution thread <br> * 5. obtain the jsapi parameter map <br> * @ author qiaowenbin * @ version 0.0.2.20150617 * @ history * 0.0.2.20150617 <br> * token <br> */public class QWeixinUtil {/*** token check * @ param request * @ param token * @ return */public static String checkToken (HttpServl EtRequest request, String token) {String signature = request. getParameter ("signature"); String timestamp = request. getParameter ("timestamp"); String nonce = request. getParameter ("nonce"); String echostr = request. getParameter ("echostr"); return checkToken (signature, timestamp, nonce, echostr, token );} /*** token verification ** @ param signature * @ param timestamp * @ param nonce * @ param echostr * @ param tok En * @ return */public static String checkToken (String signature, String timestamp, String nonce, String echostr, String token) {if (QStringUtil. allNotEmpty (signature, timestamp, nonce, echostr, token) {StringBuilder sb = new StringBuilder (); String [] tmp = {token, timestamp, nonce}; Arrays. sort (tmp); for (String s: tmp) {sb. append (s);} String pwd = QEncodeUtil. sha1 (sb. toString (); if (signature. e Quals (pwd) return echostr;} return QStringUtil. emptyStr;}/*** get access_token * @ return */public static String getaccesen en () {try {String url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = "+ QPropertiesUtil. get ("weixin. appid ") +" & secret = "+ QPropertiesUtil. get ("weixin. appsecret "); JSONObject json = new JSONObject (QHttpClientUtil. get (url); return json. getString ("access_token");} catch (Exception e) {e. printStackTrace (); return null ;}}/*** get jsticket * @ return */public static String getJsTicket (String token) {try {String url = "https: // api .Weixin.qq.com/cgi-bin/ticket/getticket? Access_token = "+ token +" & type = jsapi "; JSONObject json = new JSONObject (QHttpClientUtil. get (url); return json. getString ("ticket");} catch (Exception e) {e. printStackTrace (); return null ;}/ *** execution thread */public static void runWeixinThread () {QThreadUtil. runScheduledBySeconds (new Runnable () {public void run () {String token = getAccessToken (); QCacheUtil. putToEHCache ("weixintoken", token); QCacheUtil. putToEHCache ("weixinjsticket", getJsTicket (token) ;}, 1, 7100 );} /*** obtain the jsapi parameter map * @ param jsapi_ticket * @ param url * @ return */public static Map <String, String> getJsSignMap (String jsapi_ticket, String url) {Map <String, String> map = new HashMap <String, String> (); String nonce_str = UUID. randomUUID (). toString (); String timestamp = Long. toString (System. currentTimeMillis ()/1000 ); string s = "jsapi_ticket =" + jsapi_ticket + "& noncestr =" + nonce_str + "& timestamp =" + timestamp + "& url =" + url; String signature = QEncodeUtil. sha1 (s); map. put ("url", url); map. put ("jsapi_ticket", jsapi_ticket); map. put ("nonceStr", nonce_str); map. put ("timestamp", timestamp); map. put ("signature", signature); return map ;}}
There are three main parts:
1. token Verification
2. cache accesstoken and jsticket
3. Get js Parameters
Corresponding configuration file:
# weixin propertiesweixin.debug=falseweixin.token=yweixin.appid=yweixin.appsecret=y
In addition, you must call QWeixinUtil. runWeixinThread () when starting the service ();
[Page]
The background code to jump to the page:
private void initWeixinInfo(BlogArticleModel blog){ String jsTicket = (String) QCacheUtil.getFromEHCache("weixinjsticket"); String url = "http://uikoo9.com/blog/detail/" + blog.getStr("blog_article_code"); Map<String, String> map = QWeixinUtil.getJsSignMap(jsTicket, url); setAttr("wxdebug", QPropertiesUtil.get("weixin.debug")); setAttr("wxappid", QPropertiesUtil.get("weixin.appid")); setAttr("wxtime", map.get("timestamp")); setAttr("wxrandom", map.get("nonceStr")); setAttr("wxsign", map.get("signature")); setAttr("wxtitle", blog.getStr("blog_article_title")); setAttr("wxurl", map.get("url")); }
Corresponding page code:
<Script type = "text/javascript" src = "http://res.wx.qq.com/open/js/jweixin-1.0.0.js"> </script> <script type = "text/javascript"> wx. config ({debug :$ {wxdebug}, appId: '$ {wxappid}', timestamp :$ {wxtime}, nonceStr: '$ {wxrandom}', signature: '$ {wxsign}', jsApiList: ['onmenushareappmessage']}); wx. ready (function () {wx. onMenuShareAppMessage ({title: '$ {wxtitle}', desc: '$ {wxtitle}', link: '$ {wxurl}', imgUrl: 'http: // uikoo9.qiniudn.com/@/img/logo.png'});/* trigger: function (res) {alert ('user click to send to friends');}, success: function () {alert (1) ;}, cancel: function () {alert (2) ;}, fail: function (res) {alert (JSON. stringify (res);} wx. error (function (res) {alert (JSON. stringify (res);}); */</script>
[Notes]
You can follow the above configuration. Pay attention to the following points:
1. Make good use of wx. error, which can be used for debugging errors.
2. Make good use of debug: true in wx. config, which is also debugging and does not feel useful.
[Cheating Certification]
Open wx. error. If at this time you see:
Congratulations, you have succeeded,
However, if you click to send a message to a friend that is still not customized, congratulations,
This problem will be entangled for a long time,
At last, I will tell you that you do not have the permission to share the authentication.
Individuals cannot be authenticated. register a company ~~
[More]
More highlights: uikoo9.com