VPS参考测评推荐
专注分享VPS主机优惠信息
衡天云优惠活动
华纳云最新优惠促销活动
jtti最新优惠促销活动

实现微信分享后端接口的最新通用流程。

locvps
主机参考:VPS测评参考推荐/专注分享VPS服务器优惠信息!若您是商家可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情!
我们发布的部分优惠活动文章可能存在时效性,购买时建议在本站搜索商家名称可查看相关文章充分了解该商家!若非中文页面可使用Edge浏览器同步翻译!PayPal代付/收录合作

微信分享后台界面的简单实现

这个接口的大致流程如下:用户创建三个变量:时间戳、随机字符串和当前要共享的页面的url,然后使用其appid和APPsecret作为请求参数来获取access_token,然后根据access_token获取jsapi_ticket。对获得的jsapi -票证进行加密和验证,并对自己创建的三个变量进行签名。请注意,签名过程是根据键值的ASCII代码按升序排序并打包成json格式的数据,并传输到前台的JSON页面。具体程序如下;

公共类WeiXinShareAction扩展http servlet { private static final long serialVersionUID = 1L;私有整数main _ count = 888私有字符串标志= & quot1 & quot;private Log logger = Log factory . get Log(this . getclass());私有字符串文件路径= & quot/b . txt & quot;;protected void doGet(http servlet request请求,HttpServletResponse响应)抛出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请尝试{ 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;错误代码& quot)= = 0){ ticket = JSON object . from object(ticket stress string)。getString(& quot;门票& quot);} } } catch(Exception e){ e . printstacktrace();} if(string utils . isempty(ticket)){ JSON object . addproperty(& quot;错误代码& quot, 10002);JSON object . add property(& quot;errmsg & quot,& quot票证错误& 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);//这里是保存的点击次数。//如果没有数据库,重启服务后不会清除点击次数。//使用线程锁和IO流修改点击次数并保存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);//点击次数JSON object . add property(& quot;错误代码& 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;时间戳& quot时间戳);//timestamp JSON object . add property(& quot;非中心& quot非中心);//随机字符串JSON object . add property(& quot;签名& quot,签名);//签名response . set header(& quot;访问-控制-允许-来源& quot,& quot* & quot);this . response write(JSON object . tostring());} private void responseWrite(字符串内容,HttpServletResponse响应){ try { response . setcharacter encoding(& quot;ut F-8 & quot;);response.getWriter()。写(内容);} catch(异常e){ logger . error(& quot;微信共享操作中的responseWrite错误,e);} } } Get access _ token在开发过程中需要注意的是,微信每天限制access_token的生成次数和使用时长,是为了减轻服务器的访问压力。因为时间限制是7200s,所以我在生成令牌后2小时做出了判断并使用了相同的令牌;这只是一个小接口,所以我选择将最后一次生成时间和token保存为静态变量。

/* * *在微信上分享,获取access _ token */private string getweixinaccestoken(string【】wxinfo)throws exception {//获取当前时间longcurrent _ time = system。current time millis();//每次调用时都会判断expires_in是否过期。如果令牌时间超时,则会被检索。如果((当前时间-最后时间)/1000》= 7200){ logger . info(& quot;首次访问“+当前时间);logger . info(& quot;(当前时间-上次时间)/1000 = = = = & quot;+(当前时间-最后时间)/1000);字符串url = & quothttps://api.weixin.qq.com/cgi-bin/token?格兰特类型=客户证书。appid = & quot+wx info【0】+& quot;& amp秘密= & quot+wx info【1】;string result = this . http req execute(URL);this . logger . warn(& quot;来自微信API access token:& quot;+结果);请尝试{ last _ time = current _ timeIf(字符串实用程序。不为空(结果)){//解析respContent并获取更新后的密钥,access token = JSON object。来自对象(结果)。getString(“access _ token & quot);//保存access_token返回accessToken} } catch(Exception e){ logger . error(& quot;微信共享操作中的getAccessToken错误,e);} } else { logger . info(& quot;第二次访问“+last _ time);logger . info(& quot;(当前时间-上次时间)/1000 = = = = & quot;+(当前时间-最后时间)/1000);logger . info(& quot;来自微信API access token:& quot;+access token);返回accessToken}返回null}根据access_token获取jsapiTicket。

/* * *在微信上分享并获取JSAPI票证*/public string getsharejsapikit(string【】wxinfo)throws exception { string access _ token = this . getweixinaccestoken(wxinfo);If(字符串实用程序。isempty(access _ token)){//无法获取access token//this . logger . warn(siteid+“access token为空。");JSON object JSON object = new JSON object();JSON object . add property(& quot;错误代码& quot,& quot10000);JSON object . add property(& quot;errmsg & quot,& quot访问错误& quot);返回JSON object . tostring();}字符串url = & quothttps://api.weixin.qq.com/cgi-bin/ticket/getticket?访问令牌= & quot+访问令牌+& quot;& amptype = jsapi & quot;string jsapi ticket = this . http req execute(URL);this . logger . warn(& quot;来自微信api的jsapiTicket是:& quot+jsapi ticket);if(string utils . isnotempty(jsapi ticket)){ return jsapi ticket;}返回null}Http远程调用

私有字符串httpReqExecute(字符串URL){ String result = & quot;";default http client http client = null;请尝试{ http client = new default http client();HttpPost httppost =新的http post(URL);//execute httpresponse = http client . execute(http post);HttpEntity entity = response . getentity();如果(实体!= null & amp& ampresponse.getStatusLine()。getStatusCode()= = 200){ result = entity utils . tostring(entity,& quotut F-8 & quot;);} } catch(Exception e){ logger . error(& quot;WeiXinShareAction调用微信API失败!",e);}最后{//关闭连接并释放资源http client . getconnectionmanager()。shut down();}返回结果;}已成功返回。

来自微信API access token:{ & quot;访问令牌& 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过期时间为& quot:7200}来自微信api jsapiTicket是:{ & quot错误代码& quot:0,& quoterrmsg & quot:& quot好的,& quot门票& quot:& quothoagfkdcsgmvciy 2 vojf 9j _ us 44 qhuo 4k dgh 5u 8 wmjotuo 44 m1 hkqgesjyiyfr 9 hwrmmz -WRS B9 kldmpatrw & quot;,& quot过期时间为& quot:7200}相关文章:

在微信JS-SDK上实现Node.js的后端接口。

微信h5页面如何调用分享页面的界面?

javascript -手机在微信朋友圈分享qq好友的接口地址是什么?

相关视频:

微信公众平台界面二次开发视频教程

以上就是最新的微信分享后端接口实现的大致流程细节。更多详情请关注主机参考其他相关文章!

这几篇文章你可能也喜欢:

本文由主机参考刊发,转载请注明:实现微信分享后端接口的最新通用流程。 https://zhujicankao.com/104467.html

【腾讯云】领8888元采购礼包,抢爆款云服务器 每月 9元起,个人开发者加享折上折!
打赏
转载请注明原文链接:主机参考 » 实现微信分享后端接口的最新通用流程。
主机参考仅做资料收集,不对商家任何信息及交易做信用担保,购买前请注意风险,有交易纠纷请自行解决!请查阅:特别声明

评论 抢沙发

评论前必须登录!