发布时间:2025-12-10 16:15:00 浏览次数:11
1.登录微信开放平台,管理中心->网站应用->查看应用详情:
(1)查看微信登录接口权限是否已获得;
(2)查看授权回调域名是否是你当前使用的域名。
2.查看代码第一步请求code的时候,使用的scope是不是调用了公众号的接口scope,公众号在进行网页授权的时候scope有两种,即snsapi_base和snsapi_userinfo注意,在开放平台的应用,构建微信OAuth2.0授权登录的时候,scope为snsapi_login,其他地方跟公众号授权登录一模一样。详见官方文档。代码如下:
//微信开放平台获取用户信息public function _userInfoAuth($redirect_url){//1.准备scope为snsapi_login网页授权页面$redirecturl = urlencode($redirect_url);$snsapi_userInfo_url = "https://open.weixin.qq.com/connect/oauth2/authorize?app&redirect_uri=".$redirecturl."&response_type=code&scope=snsapi_login&state=YQJ#wechat_redirect";//2.用户手动同意授权,同意之后,获取code//页面跳转至redirect_uri/?code=CODE&state=STATE$code = $_GET["code"];if( !isset($code) ){header("Location:".$snsapi_userInfo_url);}//3.通过code换取网页授权access_token$curl = "https://api.weixin.qq.com/sns/oauth2/access_token?app&secret=".$this->_appsecret."&code=".$code."&grant_type=authorization_code";$content = $this->_request($curl);$result = json_decode($content);//4.通过access_token和openid拉取用户信息$webAccess_token = $result->access_token;$openid = $result->openid;$userInfourl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$webAccess_token."&open&lang=zh_CN ";$recontent = $this->_request($userInfourl);$userInfo = json_decode($recontent,true);return $userInfo;}谢谢观看!