微信公众号授权问题总结
2016-04-25 21:09
483 查看
授权的整个 流程梳理
1、首先获取到code
如何获取呢? https://open.weixin.qq.com/connect/oauth2/authorize?appid=".C('wxconfig')['appid']."&redirect_uri=http://www.xxx.com/&response_type=code&scope=snsapi_userinfo&state=kylife#wechat_redirect
这个授权连接地址一调用以后,微信服务器会 调转到你指定的redirect_url设置的地址,,注意:同时将code作为参数带到 redirect_url的后面
如上连接后就会 连接会变成:http://www.xxx.com/code/code码/state/kylife
2、此时获取到code以后,你在去 通过code获取 获取token和openid了
https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=上一步拿到的code&grant_type=authorization_code
3、至此,token和openid有了,就可以调用获取 微信客户 基本信息接口,拿昵称等信息了
https://api.weixin.qq.com/sns/userinfo?access_token=上边拿到的token&openid=上边拿到的openid&lang=zh_CN
OK
封装的一个简单方法 实现了以上流程:
//获取微信 客户端授权信息
function getWxinfo(){
//获取token
$token = C('wxconfig')['appid']; //appid
$secret = C('wxconfig')['secret'];//secret
$code = $_REQUEST;
if(!isset($_GET['code'])){ //如果code不存在,就header跳转一下授权地址,此时 redirect_uri=http://www.xxx.com/xx.php授权后就会get中传递了code然后再次调用本函数,此时code存在就会走下边获取到 信息了
header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=".C('wxconfig')['appid']."&redirect_uri=http://www.xxx.com/xx.php&response_type=code&scope=snsapi_userinfo&state=kylife#wechat_redirect");
}
$arr = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$token."&secret=".$secret."&code=".$code['code']."&grant_type=authorization_code");
$arr = json_decode($arr,true);
$userinfo = file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token=".$arr['access_token']."&openid=".$arr['openid']."&lang=zh_CN");
return $userinfo;
}
1、首先获取到code
如何获取呢? https://open.weixin.qq.com/connect/oauth2/authorize?appid=".C('wxconfig')['appid']."&redirect_uri=http://www.xxx.com/&response_type=code&scope=snsapi_userinfo&state=kylife#wechat_redirect
这个授权连接地址一调用以后,微信服务器会 调转到你指定的redirect_url设置的地址,,注意:同时将code作为参数带到 redirect_url的后面
如上连接后就会 连接会变成:http://www.xxx.com/code/code码/state/kylife
2、此时获取到code以后,你在去 通过code获取 获取token和openid了
https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=上一步拿到的code&grant_type=authorization_code
3、至此,token和openid有了,就可以调用获取 微信客户 基本信息接口,拿昵称等信息了
https://api.weixin.qq.com/sns/userinfo?access_token=上边拿到的token&openid=上边拿到的openid&lang=zh_CN
OK
封装的一个简单方法 实现了以上流程:
//获取微信 客户端授权信息
function getWxinfo(){
//获取token
$token = C('wxconfig')['appid']; //appid
$secret = C('wxconfig')['secret'];//secret
$code = $_REQUEST;
if(!isset($_GET['code'])){ //如果code不存在,就header跳转一下授权地址,此时 redirect_uri=http://www.xxx.com/xx.php授权后就会get中传递了code然后再次调用本函数,此时code存在就会走下边获取到 信息了
header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=".C('wxconfig')['appid']."&redirect_uri=http://www.xxx.com/xx.php&response_type=code&scope=snsapi_userinfo&state=kylife#wechat_redirect");
}
$arr = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$token."&secret=".$secret."&code=".$code['code']."&grant_type=authorization_code");
$arr = json_decode($arr,true);
$userinfo = file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token=".$arr['access_token']."&openid=".$arr['openid']."&lang=zh_CN");
return $userinfo;
}
相关文章推荐
- 第5周 经典小程序1 题 动态数组的实现
- 圆角Tablayout以及仿微信QQ小红点,数字通知
- 微信 页面分享功能
- C#开发微信门户及应用(34)--微信裂变红包
- 微信二维码支付
- 微信公众号分享内容自定义,点击链接可获点击用户的openid
- 图像分割:Grabcut算法小程序
- Android App仿微信界面切换时Tab图标变色效果的制作方法
- 微信中链接 访问公众号
- 微信模板消息
- 微信展示
- Android接入微信开发注意事项
- 微信网页调用腾讯视频去广告,视频预览截图去腾讯logo
- Java小程序之字符串倒置
- 微信企业号的JAVA开发平台
- 微信上传图片
- JAVA微信扫码支付模式二功能实现以及回调
- C#开发微信门户及应用(33)--微信现金红包的封装及使用
- Android应用加入微信分享
- JEEWX微信企业号管家,开源免费,1.0版本发布