微信小程序> 微信公众号授权,小程序向微信公众号授权

微信公众号授权,小程序向微信公众号授权

浏览量:467 时间: 来源:weixin_30244889
小程序通过webview向公众号授权
1.设置公众号授权域名

2.小程序的webview的src通过带上小程序的openid以及其他的一些参数向网页授权域名下的网站发起请求。
ps:这里有个坑!!!!如果src的url太长,是的!太长!会导致参数发不过去!什么意思呢?好比,我的请求url是https://xxx.xxx/xxx/xxx?openid={{openid}}&xx={{xx}},到达url下的接口接收到的参数openid为空,那个xx也为空。我们之前是把openid,nickname,unionid都发过去,结果全为空,找了很久才发现这个问题。
3.微信公众号静默授权
redirect_uri为授权回调地址

4.授权回调
publicclassPublicNumberCodeController:Controller{//GET:PublicNumberCodepublicActionResultIndex(){LogHelper.Info("已经到达PublicNumberCode页:"+DateTime.Now);try{//如果已经授权,直接进入页面if(!Util.isNotNull(Request["code"])){LogHelper.Info("已经授权过了");returnView();}intweixin_id=Convert.ToInt32(Request["weixin_id"]);//公众号idstringfromopenid=Request["openid"];//openidstringappcode=Request["appcode"];//appcodestringcode=Request["code"];stringstate=Request["state"];stringappid=Request["appid"];stringappsecret=Request["appsecret"];LogHelper.Info("code值:"+code);stringtoken_jsonstr=WeixinAPi.accesstoken(code,appid,appsecret);//根据code换取token,得到openid与access_tokenif(token_jsonstr.IndexOf("openid")-1&&token_jsonstr.IndexOf("access_token")-1){JObjecttoken_json=JObject.Parse(token_jsonstr);//公众号的openidstringopenid=token_json["openid"].ToString();stringaccess_token=token_json["access_token"].ToString();//插入数据关联小程序openid与公众号openid//查询是否有未发成功的模板消息,一个小时以内的LogHelper.Info("准备发送模板消息:"+DateTime.Now);发送待支付模板消息}}catch(Exceptionex){LogHelper.Error(ex.Message,ex);}LogHelper.Info("完成,到引导页:"+DateTime.Now);returnView();}ViewCode
微信授权方法
///summary///第一步:用户同意授权,获取code////summary///paramname="redirect_uri"授权后重定向的回调链接地址,请使用urlencode对链接进行处理/param///paramname="scope"应用授权作用域,snsapi_base(不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo(弹出授权页面/param///paramname="state"重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节/param///returnsnull/returnspublicstaticvoidauthorize(stringAppId,stringredirect_uri,stringscope,stringstate){HttpResponseres=HttpContext.Current.Response;stringurl="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AppId+"&redirect_uri="+HttpUtility.UrlEncode(redirect_uri)+"&response_type=code&scope="+scope+"&state="+state+"#wechat_redirect";//sys.errMessage=url;res.Redirect(url);}///summary///第二步:通过code换取网页授权access_token////summary///paramname="code"填写第一步获取的code参数/param///returnsjsonstring/returnspublicstaticstringaccesstoken(stringcode,stringappid,stringappsecret){stringurl="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+appsecret+"&code="+code+"&grant_type=authorization_code";returnUtil.MethodGET(url,"UTF-8");}///summary///第三步:通过access_token换取unionid////summary///paramname="access_token"填写第一步获取的code参数/param///paramname="openid"openid/param///returnsjsonstring/returnspublicstaticstringgetunionid(stringaccess_token,stringopenid){stringurl="https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";returnUtil.MethodGET(url,"UTF-8");}

版权声明

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

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

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