您好!欢迎来到爱源码

爱源码

热门搜索: 抖音快手短视频下载   

微信开发获取客户信息,发送模板消息。 《电影网站源码》

  • 时间:2022-10-29 03:35 编辑: 来源: 阅读:300
  • 扫一扫,手机访问
摘要:微信开发获取客户信息,发送模板消息。 《电影网站源码》
0,https://mp.weixin.qq.com/wiki?,官网地址T=resource/res_main1,获取客户信息:获取客户信息主要包括微信网页授权和UnionID机制(关注后获取)。先说网页授权方式1.1,引导授权地址。要获得代码授权,需要使用oauth2认证。首先,你需要引导客户打开地址。格式如下:https://open.weixin.qq.com/connect/oauth2/authorize? APPID = APPID & amp;重定向URI & amp;代码& amp范围=范围& ampState=State#wechat_redirect其中SCOPE为授权范围(snsapi _ userinfo),STATE为参数,可以通过STATE传递,也可以通过传统的url拼接传递(需要编码)。REDIRECT_URL是授权后的回调地址。授权后,微信会跳转到带代码的REDIRECT_URL。例如:https://open.weixin.qq.com/connect/oauth2/authorize? appid = * * * * & amp;redirect _ uri = * * * * & amp代码& ampscope = snsapi _ userinfo & ampState=#wechat_redirect1.2,交换access_token和openid的代码来请求微信接口:https://api.weixin.qq.com/sns/oauth2/access_token? APPID = APPID & amp;秘密=秘密& amp代码=代码& ampGrant_type=authorization_code微信会返回令牌(该令牌只用于步骤4获取userinfo,与其他令牌不同),该接口会返回openid、Access令牌、openid和refresh _ token;1.3刷新access_token(可选)如果accesstoken过期,可以通过refresh_token刷新令牌;请求是:https://api.weixin.qq.com/sns/oauth2/refresh_token?. APPID = APPID & amp;授权类型=刷新令牌& amp刷新令牌=刷新令牌;1.4拉客户信息(范围应该是snsapi_userinfo)。微信接口https://api.weixin.qq.com/sns/userinfo? ACCESS _ TOKEN = ACCESS _ TOKEN & amp;openid = OPENID & amplang = zh _ CN代码如下://https://API . weixin . QQ . com/SNS/oauth 2/access _ token?appid = APPID & amp秘密=秘密& amp代码=代码& ampgrant _ type = authorization _ codeString OpenID = " ";String soap_url1 =常量。WEIXIN . WX _ URL+" SNS/oauth 2/access _ token?appid=" +常量。WEIXIN . APP _ ID+" & amp;secret=" +常量。微信.秘密+" & amp;code = "+code+" & amp;grant _ type =授权_代码";string ret 1 = weixin util . connectweixin interfaceget(soap _ URL 1," ");如果(ret1!= null & amp& ampret1.trim()。length()>;0){ JSON object obj = JSON object . from object(ret 1);if (obj.get("errcode ")!= null) {//跳转到失败接口} else { OpenID = obj . getstring(" OpenID ");string access _ token = obj . getstring(" access _ token ");//https://API . weixin . QQ . com/SNS/userinfo?访问令牌=访问令牌& ampopenid = OPENID & amplang = zh _ CN String soap _ URL 2 = Constant。WEIXIN.WX_URL + "sns/userinfo?访问令牌=" +访问令牌+" & amp;OpenID = "+OpenID+" & amp;lang = zh _ CN ";string ret 2 = weixin util . connectweixin interfaceget(soap _ URL 2," ");如果(ret2!= null & amp& ampret2.trim()。length()>;0){ JSON object obj 2 = JSON object . from object(ret 2);if (obj2.get("errcode ")!= null) {//跳转到失败接口} else { string nickname = string . value of(obj 2 . get(" nickname "));string sex = string . value of(obj 2 . get(" sex "));string headimgurl = string . value of(obj 2 . get(" headimgurl "));}}}}附上WeiXinUtil代码导入Java . io . inputstream;导入Java . io . output stream;导入Java . net . httpurl connection;导入Java . net . URL;导入org . slf4j . logger;导入org . SLF 4j . logger factory;公共类WeiXinUtil {私有静态最终记录器Logger = Logger factory . get Logger(weixin util . class);公共静态字符串connectweixinterfaceget(String action,String JSON){ URL URL;试试{ URL = new URL(action);http purlconnection http =(HttpURLConnection)URL . open connection();http . setrequestmethod(" GET ");http . setrequestproperty(" Content-Type "," application/x-www-form-urlencoded ");http . setdoooutput(true);http . setdoinput(true);system . set property(" sun . net . client . defaultconnecttimeout "," 60000 ");//连接超时为60秒system . set property(" sun . net . client . defaultreadtimeout "," 60000 ");//读取超时60秒http . connect();output stream OS = http . get output stream();OS . write(JSON . getbytes(" UTF-8 "));//输入参数inputstream为= http . getinputstream();int size = is . available();byte[]JSON bytes = new byte[size];is . read(JSON bytes);字符串结果=新字符串(jsonBytes,“UTF-8”);Logger.info("请求返回结果:"+result);OS . flush();OS . close();返回结果;} catch (Exception e) {logger.info("请求微信接口失败,失败信息:"+e . getmessage()));返回“”;} }公共静态字符串connectweixinterface post(String action,String JSON){ URL URL;试试{ URL = new URL(action);http purlconnection http =(HttpURLConnection)URL . open connection();http . setrequestmethod(" POST ");http . setrequestproperty(" Content-Type "," application/x-www-form-urlencoded ");http . setdoooutput(true);http . setdoinput(true);system . set property(" sun . net . client . defaultconnecttimeout "," 60000 ");//连接超时为60秒system . set property(" sun . net . client . defaultreadtimeout "," 60000 ");//读取超时60秒http . connect();output stream OS = http . get output stream();OS . write(JSON . getbytes(" UTF-8 "));//输入参数inputstream为= http . getinputstream();int size = is . available();byte[]JSON bytes = new byte[size];is . read(JSON bytes);字符串结果=新字符串(jsonBytes,“UTF-8”);Logger.info("请求返回结果:"+result);OS . flush();OS . close();返回结果;} catch (Exception e) {logger.info("请求微信接口失败,失败信息:"+e . getmessage()));返回“”;} }}2.获取客户信息的UnionID机制2.1获取access _ token;请求微信接口:https://api.weixin.qq.com/cgi-bin/token? grant _ type = client _ credential & amp;appid = APPID & ampSecret=APPSECRET,返回accesstoken。 2.2获取客户基本信息:请求微信接口:https://api.weixin.qq.com/cgi-bin/user/info? ACCESS _ TOKEN = ACCESS _ TOKEN & amp;openid = OPENID & amplang = zh _ CN该接口返回的订阅属性为是否被关注,0未被关注,1被关注,如果被关注,则返回头像等客户信息,如果未被关注,则不返回客户信息,返回的数据如图所示:image.png代码如下:公共静态图< String,String & gtgetAccessUserInfo(String openId){ String result = weixin util . connectweixin interface(String . format(Constant。URL.USER_INFO,getAccessToken(),openId)," ");返回JSON.parseObject(result,map . class);}公共静态字符串getAccessToken(){ String result = weixin util . connectweixin interface(String . format(Constant。URL.ACCESS_TOKEN_URL,常量。WEIXIN.APP_ID,常量。WEIXIN.SECRET)、“);JSON object JSON = JSON object . from object(result);string token = strutil . getnotnullstrvalue(JSON . get(" access _ token "));返回令牌;}一般来说,UnionID机制在获取客户信息方面有局限性。比如参数需要openid,如果不关注返回的数据,就无法获取客户信息;但通过UnionID获取客户信息更容易;需要匹配场景平衡;3.发送模板消息3.1以获取openid。此步骤与Unionid机制获取accesstoken3.2发送模板消息的步骤相同。首先在微信官方账号中配置模板ID如图:image.png部分代码如下:公共类notify template { private string template _ ID;私有字符串用户;私有字符串url私有字符串优先;私有字符串关键字1;私有字符串keyword2私有字符串关键字3;私有字符串关键字4;私有字符串关键字5;私有字符串备注;私有字符串颜色;public notify template(){ super();} public notify template(String template _ id,String touser,String url,String first,String keyword1,String keyword2,String remark){ super();this.template_id =模板_ id;this.touser = touserthis.url = urlthis.first = firstthis . keyword 1 = keyword 1;this . keyword 2 = keyword 2;this.remark =备注;this.color = " # 173177} }公共静态地图& ltString,String & gtsendTemplateMsg(notify template template){ String msg = template . tojsonmsg();string result = weixin util . connectweixin interface(string . format(常量。URL.TEMPLATE_SEND_URL,getAccessToken()),msg);JSON object obj = JSON object . from object(result);地图& ltString,String & gtmap = JSONObject.toBean(obj,map . class);log . info(" getsnsoauth 2 accessstoken = = & gt;{} ",地图);返回地图;}


  • 全部评论(0)
资讯详情页最新发布上方横幅
最新发布的资讯信息
【技术支持|常见问题】1502企业站群-多域名跳转-多模板切换(2024-04-09 12:19)
【技术支持|常见问题】1126完美滑屏版视频只能显示10个(2024-03-29 13:37)
【技术支持|常见问题】响应式自适应代码(2024-03-24 14:23)
【技术支持|常见问题】1126完美滑屏版百度未授权使用地图api怎么办(2024-03-15 07:21)
【技术支持|常见问题】如何集成阿里通信短信接口(2024-02-19 21:48)
【技术支持|常见问题】算命网微信支付宝产品名称年份在哪修改?风水姻缘合婚配对_公司起名占卜八字算命算财运查吉凶源码(2024-01-07 12:27)
【域名/主机/服务器|】帝国CMS安装(2023-08-20 11:31)
【技术支持|常见问题】通过HTTPs测试Mozilla DNS {免费源码}(2022-11-04 10:37)
【技术支持|常见问题】别告诉我你没看过邰方这两则有思想的创意广告! (2022-11-04 10:37)
【技术支持|常见问题】你正确使用https了吗? [php源码](2022-11-04 10:37)

联系我们
Q Q:375457086
Q Q:526665408
电话:0755-84666665
微信:15999668636
联系客服
企业客服1 企业客服2 联系客服
86-755-84666665
手机版
手机版
扫一扫进手机版
返回顶部