这里的网页授权是指在微信里浏览网页时,通过授权让网页去访问微信用户的信息.

可以从"微信官方后台管理->开发->接口权限->网页服务->网页帐号->网页授权获取用户基本信息"这里设置网页授权回调域名,需要注意的是,这个域名,不带http或https,直接域名,而不用带子目录.比如我的某个网页http://szuzsq.tunnel.qydev.com/weixin/OAuth2.php,那我设置网页授权回调域名,则为szuzsq.tunnel.qydev.com,记得不能是szuzsq.tunnel.qydev.com/weixin.

        从官方文档里面可知道,有两种scope.

1).以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的.需要用户已关注公众号.

2).以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的.但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息.

        以下,用个实例,详细讲解,这个功能的实现步骤.

1).在我的实例中,我是在菜单中进入view的.网址为"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf649ff600f132a41&redirect_uri=http://szuzsq.tunnel.qydev.com/weixin/OAuth2.php&response_type=code&scope=snsapi_base&state=hello_world_123#wechat_redirect"

当scope为snsapi_userinfo且没注册公众号时,用弹出下面熟悉的界面:

python 微信网页授权同意 微信打开网页授权_公众号

 

2).微信跳转到设定的redirect_uri时,会带上code和state两个参数.例如,我的是:http://szuzsq.tunnel.qydev.com/weixin/OAuth2.php?code=234523423sdfgsdfg34sfdg&state=hello_world_123

3).此时在这个页面里,能得到code.通过code可获得access_token.

    到这里,又有一个坑了,用户同意授权,获取code,每次用户授权,获得的code也不一样.而且code只能使用一次,5分钟未被使用自动过期.所以通过code获得的access_token也应该是不同的.并且需要注意这是专门针对,网页授权access_token,与基础支持中的access_token不同.我理解,这个网页授权access_token没有基础的access_token那样,有访问限制.所以应该是每次用户同意授权,都可以获得一次.官方文档里说,access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,refresh_token有效期为30天.当refresh_token失效之后,需要用户重新授权.

4).刷新access_token(如果需要) 5).拉取用户信息

 

我的代码如下:

 

<?php
//文件名: http://szuzsq.tunnel.qydev.com/weixin/OAuth2.php

include_once 'utils.php';
header('content-type: application/json; charset=utf-8');

$code = $_REQUEST["code"];
$state = $_REQUEST["state"];
echo "code:" . $code . "<p/>";
echo "state:" . $state . "<p/>";

$result = get_token($code);
$json = json_decode($result);
echo "$result<p/>";
echo "<p/><p/>";

$result = verfy_token($json->access_token, $json->openid);
//$json = json_decode($result);
echo "$result<p/>";
echo "<p/><p/>";

$result = refresh_token($json->refresh_token);
$json = json_decode($result);
echo "$result<p/>";
echo "<p/><p/>";

$result = get_user_info($json->access_token, $json->openid);
$json = json_decode($result);
echo "$result<p/>";
echo "<p/><p/>";

function get_token($code) { //通过code换取网页授权access_token
	$appid = "wxf649ff600f132a41";
	$appsecret = "5baf64562b15223f919165d083f9b889";
	$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
	$result = curl_http_get($url);//$result = file_get_contents($url);
	$json = json_decode($result);
	return $result;
}

function refresh_token($refresh_token) { //刷新access_token
	$appid = "wxf649ff600f132a41";
	$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=$appid&grant_type=refresh_token&refresh_token=$refresh_token";
	$result = curl_http_get($url); //$result = file_get_contents($url);
	$json = json_decode($result);
	return $result;
}

function verfy_token($access_token, $openid) { //检验授权凭证access_token是否有效
	$url = "https://api.weixin.qq.com/sns/auth?access_token=$access_token&openid=$openid";
	$result = curl_http_get($url); //$result = file_get_contents($url);
	//$json = json_decode($result);
	return $result;
}

function get_user_info($ACCESS_TOKEN, $openid) { //获取用户信息
	$url = "https://api.weixin.qq.com/sns/userinfo?access_token=$ACCESS_TOKEN&openid=$openid&lang=zh_CN";
	$result = curl_http_get($url);
	echo $result;
}
?>

 

 

我的结果如下(我排了下版):

code:011LVgXe0PqzZE1tveYe01NmXe0LVgXm state:hello_world_123

 

//get_token($code); //通过code换取网页授权access_token
{
	"access_token": "0trdbmDfSG9fQi4bzR4DJsAPKQBByUuY9CK1jSK5G4PbB-uQ4zBUd_afpUuMtY_QNbuV29JiZ-2-jgtYqGniZ1zU3M4yFC2L2jzGwbwvH3E",
	"expires_in": 7200,
	"refresh_token": "ftCJYWeleqbrFFqJnzay2uXmgQQkOdBldCIjSJMRwydyh84HV1Yj42UWUQrsH_IRZp_Kr372NZaNZok6_52PdzAdMZHh13lG0VbvnftqFc8",
	"openid": "oe5SXv71irmrQ0TH-JxA-z_hmYLE",
	"scope": "snsapi_base"
}

//verfy_token($access_token, $openid); //检验授权凭证access_token是否有效
{"errcode": 0, "errmsg": "ok"}

//refresh_token($refresh_token); //刷新access_token
{
	"openid": "oe5SXv71irmrQ0TH-JxA-z_hmYLE",
	"access_token": "0trdbmDfSG9fQi4bzR4DJsAPKQBByUuY9CK1jSK5G4PbB-uQ4zBUd_afpUuMtY_QNbuV29JiZ-2-jgtYqGniZ1zU3M4yFC2L2jzGwbwvH3E",
	"expires_in": 7200,
	"refresh_token": "ftCJYWeleqbrFFqJnzay2uXmgQQkOdBldCIjSJMRwydyh84HV1Yj42UWUQrsH_IRZp_Kr372NZaNZok6_52PdzAdMZHh13lG0VbvnftqFc8",
	"scope": "snsapi_base,snsapi_userinfo,"
}

//get_user_info($ACCESS_TOKEN, $openid); //获取用户信息
{
	"openid": "oe5SXv71irmrQ0TH-JxA-z_hmYLE",
	"nickname": "rené",
	"sex": 1,
	"language": "zh_CN",
	"city": "深圳",
	"province": "广东",
	"country": "中国",
	"headimgurl": "http://wx.qlogo.cn/mmopen/21ic7tNxlFXCsIUwkk6ehSfYyDROqMb6htNywiagn9mFibxd7yMbibgbiaE4giajXiclgMh9gu55icjjFfTpHGImMdhcQdA7mHFEdk5z/0",
	"privilege": []
}