微信公众号里怎么添加小程序-一、微信公众号和小程序开发(获取TOKENAND推送)-微信关联小程序
1.“原理:你去异步访问他、带上相应参数、成功的话就能下载一个文件、这个文件里面就有相应的token”
1.公众号1.调用service方法(从网络Url中下载文件):2.https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=唯一标识&secret=唯一密码
2.读取文件中的TokenpublicStringUpdateToken(HttpServletRequestrequest)throwsIOException{StringtokenURL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2b1927149d977111&secret=bf18aa42e02f697211a496882c7c1111";FileTokenFile=newFile(this.getClass().getResource("").getFile()+"/token.txt");HTTPUtils.downLoadFromUrl(tokenURL,"token.txt",TokenFile.getParent());System.out.println(TokenFile.getAbsolutePath());ListStringstringList=IOUtils.readLines(newFileInputStream(TokenFile));Stringtoken=stringList.get(0);String[]tokenParse=token.substring(1,token.length()).replaceAll(""","").split(",");String[]result=tokenParse[0].split(":");if(result[0].equals("access_token")){System.out.println("获取token成功");System.out.println(result[1]);request.getServletContext().setAttribute("WeachatToken",result[1]);returnresult[1];}else{System.out.println("获取token失败");return"获取token失败";}}2.小程序三、获取Openid(对公众号和小程序的用户进行操作)3.“其实原理就是、你去重定向他、带上他想要的东西。然后他会吸收掉参数、然后来访问你的接口”
1.进行消息推送(公众号)4.1.拿到code:
5.进行重定向https://open.weixin.qq.com/connect/oauth2/authorize?appid=唯一标识&redirect_uri="+回调地址(给需要openid的方法)+"&response_type=code&scope=snsapi_userinfo&state=13392876810#wechat_redirect=asd
6.2.拿到Openid:
7.写一个API接口接收code,访问https://api.weixin.qq.com/sns/oauth2/access_token?appid=唯一标识&secret=唯一密码&code="+第一步会带来的参数+"&grant_type=authorization_code
2.进行消息推送(小程序)END:codding项目地址8.https://git.dev.tencent.com/a83841070/WechatAPI_BackEnd_TOKEN_OPENID.git
9.git@git.dev.tencent.com:a83841070/WechatAPI_BackEnd_TOKEN_OPENID.git
TAG标签: 微信小程序微信公众号里怎么添加小程序微信关联小程序
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。

