微信小程序> 微信公众号里怎么添加小程序-一、微信公众号和小程序开发(获取TOKENAND推送)-微信关联小程序

微信公众号里怎么添加小程序-一、微信公众号和小程序开发(获取TOKENAND推送)-微信关联小程序

浏览量:3238 时间: 来源:蓝冰印
WechatAPI_BackEnd_TOKEN_OPENID一、前期准备有一台公网IP的服务器注册域名并进行备案(保证合法开启80端口)在微信公众号平台AND小程序平台填写服务器相关配置和回调地址拉取Demo、数字签名等等进行微信认证可以直接用80端口运行、也可以在有域名的情况下进行绑定其他端口。#二、获取Token(用于对公众号和小程序进行操作)

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

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎