您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

最新整理出的微信分享后端接口实现的大致流程

2025/7/6 1:13:01发布23次查看
微信分享后台接口简单实现
此接口大致的流程是:用户创建时间戳,随机字符串,当前需要分享的页面的url三个变量,接着将自己的appid和appsecret作为请求参数获取access_token,再根据access_token获取jsapi_ticket, 并将获取的jsapi-ticket进行加密、校验以及自己创建的三个变量进行签名,注意签名过程案按照 key 值 ascii 码升序排序封装成json格式的数据传送到前台js页面,具体程序如下;
public class weixinshareaction extends httpservlet { private static final long serialversionuid = 1l; private integer main_count = 888; private string flag = "1"; private log logger = logfactory.getlog(this.getclass()); private string filepath = "/b.txt"; protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { jsonobject jsonobject = new jsonobject(); string ticket = null; string[] wxinfo = new string[] { "wx007344f87ae48300", "5442edc712b6846bdd1c058b7f2318fe" }; weixinutil wxu = new weixinutil(); string ticketresstring; try { ticketresstring = wxu.getsharejsapiticket(wxinfo); if (stringutils.isnotempty(ticketresstring)) { jsonobject ticketjsonobject = jsonobject.fromobject(ticketresstring); if (ticketjsonobject.getint("errcode") == 0) { ticket = jsonobject.fromobject(ticketresstring).getstring("ticket"); } } } catch (exception e) { e.printstacktrace(); } if (stringutils.isempty(ticket)) { jsonobject.addproperty("errcode", 10002); jsonobject.addproperty("errmsg", "ticket_error"); this.responsewrite(jsonobject.tostring(), response); return; } string noncestr = this.createnoncestr(); int timestamp = this.createtimestamp(); string requestrefererurl = request.getheader("referer"); flag = request.getparameter("temp"); logger.info("flag--------------" + flag); //这里是保存点击次数 //没有数据库的情况下 保证服务重启后点击次数不清零 //利用线程锁 使用io流 对点击次数进行修改保存 thread_readfile thf4 = new thread_readfile(); thf4.start(); logger.warn("requestrefererurl: " + requestrefererurl); string signature = this.createsignature(noncestr, ticket, timestamp, requestrefererurl); jsonobject.addproperty("countnum", main_count);//点击次数 jsonobject.addproperty("errcode", 0);// jsonobject.addproperty("errmsg", "");// jsonobject.addproperty("wxuser", wxinfo[0]); // appid jsonobject.addproperty("timestamp", timestamp);//时间戳 jsonobject.addproperty("noncestr", noncestr);//随机字符串 jsonobject.addproperty("signature", signature);//签名 response.setheader("access-control-allow-origin", "*"); this.responsewrite(jsonobject.tostring(), response); } private void responsewrite(string content, httpservletresponse response) { try { response.setcharacterencoding("utf-8"); response.getwriter().write(content); } catch (exception e) { logger.error("responsewrite error in weixinshareaction", e); } }}
获取access_token;这里开发过程中要注意微信为了减轻对服务器的访问压力 限制了access_token每天的生成次数 以及使用时长;
由于限制时长为7200s 于是做了一个判断 再生成一个token后的2小时用同一个token ;
这里仅仅只是一个小接口 于是选择将 最近一次的生成时间 以及 token 存为静态变量,
/** * 微信分享,获取access_token */ private string getweixinaccesstoken(string[] wxinfo) throws exception { //得到当前时间 long current_time = system.currenttimemillis(); // 每次调用,判断expires_in是否过期,如果token时间超时,重新获取,expires_in有效期为7200 if ((current_time - last_time) / 1000 >= 7200) { logger.info("第一次访问"+current_time); logger.info("(current_time - last_time) / 1000===="+(current_time - last_time) / 1000); string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxinfo[0] + "&secret=" + wxinfo[1]; string result = this.httpreqexecute(url); this.logger.warn("from weixin api accesstoken: " + result); try { last_time = current_time; if (stringutils.isnotempty(result)) {// 解析respcontent,并获取其中的更新的key, accesstoken = jsonobject.fromobject(result).getstring("access_token");// 保存access_token return accesstoken; } } catch (exception e) { logger.error("getaccesstoken error in weixinshareaction", e); } }else{ logger.info("第二次访问"+last_time); logger.info("(current_time - last_time) / 1000===="+(current_time - last_time) / 1000); logger.info("from weixin api accesstoken:"+accesstoken); return accesstoken; } return null; }
根据access_token获取jsapiticket
/** * 微信分享,获取jsapiticket */ public string getsharejsapiticket(string[] wxinfo) throws exception { string access_token = this.getweixinaccesstoken(wxinfo); if (stringutils.isempty(access_token)) { // 获取 accesstoken 失败 //this.logger.warn(siteid + " accesstoken is empty."); jsonobject jsonobject = new jsonobject(); jsonobject.addproperty("errcode", "10000"); jsonobject.addproperty("errmsg", "access_error"); return jsonobject.tostring(); } string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi"; string jsapiticket = this.httpreqexecute(url); this.logger.warn(" from weixin api jsapiticket is: " + jsapiticket); if (stringutils.isnotempty(jsapiticket)) { return jsapiticket; } return null; }
http远程调用
private string httpreqexecute(string url) { string result = ""; defaulthttpclient httpclient = null; try { httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); // 执行 httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); if (entity != null && response.getstatusline().getstatuscode() == 200) { result = entityutils.tostring(entity, "utf-8"); } } catch (exception e) { logger.error(" weixinshareaction 调用微信 api 失败!", e); } finally {// 关闭连接,释放资源 httpclient.getconnectionmanager().shutdown(); } return result; }
返回成功
from weixin api accesstoken: {"access_token":"12_9ugvn7tfvtvf_7r4lq4v9w9-pqdzpqwxvjfspof3hv3j5_xfwqwqauj4n9-zmikc1_ocp0ipbxjpzr-ty8xzg8qmev2qvukfz5_np7kjab05mx9msxrg0flpaamjonrrh5wxseffkhec0_bdhfkjafaxva","expires_in":7200} from weixin api jsapiticket is: {"errcode":0,"errmsg":"ok","ticket":"hoagfkdcsgmvciy2vojf9j_us44qhuo4kdgh5u8ewmjoctuo44m1hkqgesjyiyfr9hwrmmz-wrsb9kldmpatrw","expires_in":7200}
相关文章:
实现node.js在微信js-sdk后端接口
微信端h5页面如何调用分享页面的接口
javascript - 手机端的分享至微信 微信朋友圈 qq好友的接口地址?
相关视频:
微信公众平台接口二次开发视频教程
以上就是最新整理出的微信分享后端接口实现的大致流程的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product