| 主機(jī)參考:VPS測(cè)評(píng)參考推薦/專(zhuān)注分享VPS服務(wù)器優(yōu)惠信息!若您是商家可以在本站進(jìn)行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務(wù),查看詳情! |
| 我們發(fā)布的部分優(yōu)惠活動(dòng)文章可能存在時(shí)效性,購(gòu)買(mǎi)時(shí)建議在本站搜索商家名稱可查看相關(guān)文章充分了解該商家!若非中文頁(yè)面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
微信分享后臺(tái)界面的簡(jiǎn)單實(shí)現(xiàn)
這個(gè)接口的大致流程如下:用戶創(chuàng)建三個(gè)變量:時(shí)間戳、隨機(jī)字符串和當(dāng)前要共享的頁(yè)面的url,然后使用其appid和APPsecret作為請(qǐng)求參數(shù)來(lái)獲取access_token,然后根據(jù)access_token獲取jsapi_ticket。對(duì)獲得的jsapi -票證進(jìn)行加密和驗(yàn)證,并對(duì)自己創(chuàng)建的三個(gè)變量進(jìn)行簽名。請(qǐng)注意,簽名過(guò)程是根據(jù)鍵值的ASCII代碼按升序排序并打包成json格式的數(shù)據(jù),并傳輸?shù)角芭_(tái)的JSON頁(yè)面。具體程序如下;
公共類(lèi)WeiXinShareAction擴(kuò)展http servlet { private static final long serialVersionUID = 1L;私有整數(shù)main _ count = 888私有字符串標(biāo)志= & quot1 & quot;private Log logger = Log factory . get Log(this . getclass());私有字符串文件路徑= & quot/b . txt & quot;;protected void doGet(http servlet request請(qǐng)求,HttpServletResponse響應(yīng))拋出ServletException,io exception { JSON object JSON object = new JSON object();String ticket = nullString【】wx info = new String【】{ & quot;wx 007344 f 87 AE 48300 & quot;,& quot5442 EDC 712 b 6846 BDD 1c 058 b7f 2318 Fe & quot;};weixiutil wxu = new weixiutil();字符串ticketResString請(qǐng)嘗試{ ticket strestring = wxu . getsharejsapitock(wx info);if(string utils . isnotempty(ticket stress string)){ JSON object ticket JSON object = JSON object . from object(ticket stress string);if(ticket JSON object . getint(& quot;錯(cuò)誤代碼& quot)= = 0){ ticket = JSON object . from object(ticket stress string)。getString(& quot;門(mén)票& quot);} } } catch(Exception e){ e . printstacktrace();} if(string utils . isempty(ticket)){ JSON object . addproperty(& quot;錯(cuò)誤代碼& quot, 10002);JSON object . add property(& quot;errmsg & quot,& quot票證錯(cuò)誤& quot);this . response write(JSON object . tostring());返回;} String non centr = this . createnoncentr();int timestamp = this . create timestamp();string requestrefereurl = request . get header(& quot;referer & quot);flag = request . getparameter(& quot;溫度& quot);logger . info(& quot;flag -------------& quot;+flag);//這里是保存的點(diǎn)擊次數(shù)。//如果沒(méi)有數(shù)據(jù)庫(kù),重啟服務(wù)后不會(huì)清除點(diǎn)擊次數(shù)。//使用線程鎖和IO流修改點(diǎn)擊次數(shù)并保存thread _ readfile th F4 = new thread _ readfile();THF 4 . start();logger . warn(& quot;requestrefereurl:& quot;+requestrefereurl);string signature = this . create signature(non centr,ticket,timestamp,requestrefereurl);JSON object . add property(& quot;countNum & quot,main _ count);//點(diǎn)擊次數(shù)JSON object . add property(& quot;錯(cuò)誤代碼& quot, 0);//JSON object . add property(& quot;errmsg & quot,& quot");//JSON object . add property(& quot;wxuser & quot,wx info【0】);//appId JSON object . add property(& quot;時(shí)間戳& quot時(shí)間戳);//timestamp JSON object . add property(& quot;非中心& quot非中心);//隨機(jī)字符串JSON object . add property(& quot;簽名& quot,簽名);//簽名response . set header(& quot;訪問(wèn)-控制-允許-來(lái)源& quot,& quot* & quot);this . response write(JSON object . tostring());} private void responseWrite(字符串內(nèi)容,HttpServletResponse響應(yīng)){ try { response . setcharacter encoding(& quot;ut F-8 & quot;);response.getWriter()。寫(xiě)(內(nèi)容);} catch(異常e){ logger . error(& quot;微信共享操作中的responseWrite錯(cuò)誤,e);} } } Get access _ token在開(kāi)發(fā)過(guò)程中需要注意的是,微信每天限制access_token的生成次數(shù)和使用時(shí)長(zhǎng),是為了減輕服務(wù)器的訪問(wèn)壓力。因?yàn)闀r(shí)間限制是7200s,所以我在生成令牌后2小時(shí)做出了判斷并使用了相同的令牌;這只是一個(gè)小接口,所以我選擇將最后一次生成時(shí)間和token保存為靜態(tài)變量。
/* * *在微信上分享,獲取access _ token */private string getweixinaccestoken(string【】wxinfo)throws exception {//獲取當(dāng)前時(shí)間longcurrent _ time = system。current time millis();//每次調(diào)用時(shí)都會(huì)判斷expires_in是否過(guò)期。如果令牌時(shí)間超時(shí),則會(huì)被檢索。如果((當(dāng)前時(shí)間-最后時(shí)間)/1000》= 7200){ logger . info(& quot;首次訪問(wèn)“+當(dāng)前時(shí)間);logger . info(& quot;(當(dāng)前時(shí)間-上次時(shí)間)/1000 = = = = & quot;+(當(dāng)前時(shí)間-最后時(shí)間)/1000);字符串url = & quothttps://api.weixin.qq.com/cgi-bin/token?格蘭特類(lèi)型=客戶證書(shū)。appid = & quot+wx info【0】+& quot;& amp秘密= & quot+wx info【1】;string result = this . http req execute(URL);this . logger . warn(& quot;來(lái)自微信API access token:& quot;+結(jié)果);請(qǐng)嘗試{ last _ time = current _ timeIf(字符串實(shí)用程序。不為空(結(jié)果)){//解析respContent并獲取更新后的密鑰,access token = JSON object。來(lái)自對(duì)象(結(jié)果)。getString(“access _ token & quot);//保存access_token返回accessToken} } catch(Exception e){ logger . error(& quot;微信共享操作中的getAccessToken錯(cuò)誤,e);} } else { logger . info(& quot;第二次訪問(wèn)“+last _ time);logger . info(& quot;(當(dāng)前時(shí)間-上次時(shí)間)/1000 = = = = & quot;+(當(dāng)前時(shí)間-最后時(shí)間)/1000);logger . info(& quot;來(lái)自微信API access token:& quot;+access token);返回accessToken}返回null}根據(jù)access_token獲取jsapiTicket。
/* * *在微信上分享并獲取JSAPI票證*/public string getsharejsapikit(string【】wxinfo)throws exception { string access _ token = this . getweixinaccestoken(wxinfo);If(字符串實(shí)用程序。isempty(access _ token)){//無(wú)法獲取access token//this . logger . warn(siteid+“access token為空。");JSON object JSON object = new JSON object();JSON object . add property(& quot;錯(cuò)誤代碼& quot,& quot10000);JSON object . add property(& quot;errmsg & quot,& quot訪問(wèn)錯(cuò)誤& quot);返回JSON object . tostring();}字符串url = & quothttps://api.weixin.qq.com/cgi-bin/ticket/getticket?訪問(wèn)令牌= & quot+訪問(wèn)令牌+& quot;& amptype = jsapi & quot;string jsapi ticket = this . http req execute(URL);this . logger . warn(& quot;來(lái)自微信api的jsapiTicket是:& quot+jsapi ticket);if(string utils . isnotempty(jsapi ticket)){ return jsapi ticket;}返回null}Http遠(yuǎn)程調(diào)用
私有字符串httpReqExecute(字符串URL){ String result = & quot;";default http client http client = null;請(qǐng)嘗試{ http client = new default http client();HttpPost httppost =新的http post(URL);//execute httpresponse = http client . execute(http post);HttpEntity entity = response . getentity();如果(實(shí)體!= null & amp& ampresponse.getStatusLine()。getStatusCode()= = 200){ result = entity utils . tostring(entity,& quotut F-8 & quot;);} } catch(Exception e){ logger . error(& quot;WeiXinShareAction調(diào)用微信API失敗!",e);}最后{//關(guān)閉連接并釋放資源http client . getconnectionmanager()。shut down();}返回結(jié)果;}已成功返回。
來(lái)自微信API access token:{ & quot;訪問(wèn)令牌& quot:& quot12 _ 9 ugvn 7 tfvtvf _ 7 r4lq 4v 9 w 9-pqdzpqwxvjfspof 3 HV 3j 5 _ xfwqwqauj 4n 9-zmi KC 1 _ OCP 0 ipbxjpzr -ty 8 xzg 8 qmev 2 qvukfz 5 _ NP 7 kjab 05 mx9 msxrg 0 flpaamjonrrh 5 wxseffkhec 0 _ bdhfkjafax va & quot;,& quot過(guò)期時(shí)間為& quot:7200}來(lái)自微信api jsapiTicket是:{ & quot錯(cuò)誤代碼& quot:0,& quoterrmsg & quot:& quot好的,& quot門(mén)票& quot:& quothoagfkdcsgmvciy 2 vojf 9j _ us 44 qhuo 4k dgh 5u 8 wmjotuo 44 m1 hkqgesjyiyfr 9 hwrmmz -WRS B9 kldmpatrw & quot;,& quot過(guò)期時(shí)間為& quot:7200}相關(guān)文章:
在微信JS-SDK上實(shí)現(xiàn)Node.js的后端接口。
微信h5頁(yè)面如何調(diào)用分享頁(yè)面的界面?
javascript -手機(jī)在微信朋友圈分享qq好友的接口地址是什么?
相關(guān)視頻:
微信公眾平臺(tái)界面二次開(kāi)發(fā)視頻教程
以上就是最新的微信分享后端接口實(shí)現(xiàn)的大致流程細(xì)節(jié)。更多詳情請(qǐng)關(guān)注主機(jī)參考其他相關(guān)文章!
這幾篇文章你可能也喜歡:
本文由主機(jī)參考刊發(fā),轉(zhuǎn)載請(qǐng)注明:實(shí)現(xiàn)微信分享后端接口的最新通用流程。 http://www.k2270.com/104467.html
主機(jī)參考

















評(píng)論前必須登錄!
注冊(cè)