微信公众号开发---OAuth2.0网页授权

原文:https://blog.csdn.net/qq_28506819/article/details/78008390 
 

OAuth允许用户提供一个令牌,而不是用户名和密码来访问它们存放在特定服务器上的数据,每一个令牌授权一个特定的网站在特定时段内访问特定的资源。

授权过程如下:

1、引导用户进入授权页面同意授权,获取code 

2、通过code换取网页授权access_token(与基础支持中的access_token不同) 

3、如果需要,开发者可以刷新网页授权access_token,避免过期 

4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制) 

1、配置授权回调页面域名
在测试号权限列表中找到OAuth2.0网页授权,然后点击修改,填写自己的域名,注意不能带http://,如果通过,会提示“通过安全检测”的字样。

2、用户授权获取code
接口:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

扫描二维码关注公众号,回复: 5121832 查看本文章

scope的两种区别:
snsapi_base:不弹出授权页面,直接跳转,只能获取用户openid
snsapi_userinfo:弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息。

redirect_uri:授权后重定向的回调链接地址,请使用urlencode对链接进行处理。

尤其注意:由于授权操作安全等级较高,所以在发起授权请求时,微信会对授权链接做正则强匹配校验,如果链接的参数顺序不对,授权页面将无法正常访问。

在根目录下新建一个文件夹oauth,并在该文件夹下新建oauth2.php。该文件就是,接口中redirect_uri域名回调页面。

具体获取code的方法步骤是:提供用户请求授权页面的URL,也就是以上的接口,用户点击授权页面URL向服务器发起请求,服务器询问用户是否同意(scope为snsapi_base时无此步骤),用户同意(scope为snsapi_base时无此步骤),服务器将code通过回调传给公众账号开发者。这里我们只是简单的输出以下code。
oauth2.php

<?php
if (isset($_GET['code'])){
    echo "code:".$_GET['code']."<br>";
    echo "state:".$_GET["state"];
}else {
    echo "no code";
}


然后在index.php中向用户发送授权URL,如下所示。
index.php

<?php
@header('Content-type: text/html;charset=UTF-8');
//设置时区
date_default_timezone_set("Asia/Shanghai");
//定义TOKEN常量,这里的"weixin"就是在公众号里配置的TOKEN
 
require_once("Utils.php");
//打印请求的URL查询字符串到query.xml
Utils::traceHttp();
 
$wechatObj = new wechatCallBackapiTest();
$wechatObj->responseMsg();
 
class wechatCallBackapiTest
{
    public function responseMsg()
    {
        //获取post过来的数据,它一个XML格式的数据
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        //将数据打印到log.xml
        Utils::logger($postStr);
        if (!empty($postStr)) {
            //将XML数据解析为一个对象
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);
            //消息类型分离
            switch($RX_TYPE)
            {
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
                default:
                    $result = "";
                    break;
            }
            Utils::logger($result, '公众号');
            echo $result;
        }else {
            echo "";
            exit;
        }
    }
 
    private function receiveText($object)
    {
        $appid = "wx07fff9c79a410b69";
        $redirect_uri = urlencode("http://weiweiyi.duapp.com/oauth/oauth2.php");
 
        $keyword = trim($object->Content);
        if(strstr($keyword, "base")){
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=".
                "$redirect_uri&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
            $content = "用户授权snsapi_base实现:<a href='$url'>单击这里体验OAuth授权</a>";
        }else if (strstr($keyword, "userinfo")){
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=".
                "$redirect_uri&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
            $content = "用户授权snsapi_userInfo实现:<a href='$url'>单击这里体验OAuth授权</a>";
        }else{
            $content = "";
        }
        $result = $this->transmitText($object, $content);
        return $result;
    }
    /**
     * 回复文本消息
     */
    private function transmitText($object, $content)
    {
        $xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime><![CDATA[%s]]></CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[%s]]></Content>
</xml>";
        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }
}


向公众号分别发送“base”和"baseInfo",则返回如图所示:


点击base链接后,直接重定向到oauth2.php页面,可以看到返回的参数中不仅有code,还返回了url中填写的state字段。


点击userinfo,则先返回如下页面,需要用户点击“确认”后才能重定向到oauth2.php。


3、使用code换取access_token
如果网页授权作用域是snsapi_base,则本步骤获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程到此为止。注意这里的access_token和基础接口中的access_token不同。
接口:https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code。
get_token.php

<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
 
$appid = "wx07fff9c79a410b69";
$appsecret = "092c0c0c5bd62f66b76ad241612915fb";
$code = "0016Q5kn0zdDFp1E3rjn0VUjkn06Q5km";
 
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
    ."appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
$result = Utils::https_request($url);
echo $result;


如果是base,授权流程到此结束,返回:

{
    "access_token": "b3e1GZdT1E-sjKzeKRCr9XUE6IQglkBBxrFXdsmZ8DVW4O5t16EXbIxCoob6pGXwA5Z9JubOZnIytGcM5xC20g",
    "expires_in": 7200,
    "refresh_token": "yJkiFmmRVq5Kst6PiZpwGPvJh0bcegccx-KFIZEIwYKRmdiLC5dG8-iMRkjl1Stf8cSrHjDauzZtEGNHlnGckA",
    "openid": "o4WmZ0h-4huBUVQUczx2ezaxIL9c",
    "scope": "snsapi_base"
}


如果是userinfo,返回:

{
    "access_token": "iUIP_RnPmjVICZtmq6fFRcslRD1yJax3IkeT_fXKFlDv5W_9y5JS4Z4QgC1W33Qi2BbQ5pWLWt-6LYT7u1Egvg",
    "expires_in": 7200,
    "refresh_token": "rKwY7NF0BqfSpLVwmVO-htyvlrFWQVRmCdimoaLG2JiHz8wEJZ2H7fcQ5wtJylixBt-dCENgasbaSs8_7M-Kmw",
    "openid": "o4WmZ0h-4huBUVQUczx2ezaxIL9c",
    "scope": "snsapi_userinfo"
}


4、刷新access_token(可选)
由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。
接口:https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN。
get_refresh_token.php

<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
 
$appid = "wx07fff9c79a410b69";
$appsecret = "092c0c0c5bd62f66b76ad241612915fb";
$refresh_token = "rKwY7NF0BqfSpLVwmVO-htyvlrFWQVRmCdimoaLG2JiHz8wEJZ2H7fcQ5wtJylixBt-dCENgasbaSs8_7M-Kmw";
 
$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?"
    ."appid=$appid&grant_type=refresh_token&refresh_token=$refresh_token";
$result = Utils::https_request($url);
echo $result;


返回:

{
    "openid": "o4WmZ0h-4huBUVQUczx2ezaxIL9c",
    "access_token": "iUIP_RnPmjVICZtmq6fFRcslRD1yJax3IkeT_fXKFlDv5W_9y5JS4Z4QgC1W33Qi2BbQ5pWLWt-6LYT7u1Egvg",
    "expires_in": 7200,
    "refresh_token": "rKwY7NF0BqfSpLVwmVO-htyvlrFWQVRmCdimoaLG2JiHz8wEJZ2H7fcQ5wtJylixBt-dCENgasbaSs8_7M-Kmw",
    "scope": "snsapi_base,snsapi_userinfo,"
}


5、使用access_token获取用户信息
接口:https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN。
get_userinfo.php

<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
 
$access_token = "iUIP_RnPmjVICZtmq6fFRcslRD1yJax3IkeT_fXKFlDv5W_9y5JS4Z4QgC1W33Qi2BbQ5pWLWt-6LYT7u1Egvg";
$openid = "o4WmZ0h-4huBUVQUczx2ezaxIL9c";
 
$url = "https://api.weixin.qq.com/sns/userinfo?"
    ."access_token=$access_token&openid=$openid&lang=zh_CN ";
$result = Utils::https_request($url);
echo $result;


返回:

{
    "openid": "o4WmZ0h-4huBUVQUczx2ezaxIL9c",
    "nickname": "Promise",
    "sex": 1,
    "language": "zh_CN",
    "city": "",
    "province": "",
    "country": "",
    "headimgurl": "http://wx.qlogo.cn/mmopen/vi_32/um6ptBDhpau47ctyJHMakZgyHJsYHzjMfouyWqP6DNxNEPLf2uk6V6TBNnsbanrUcABJiaEa74W8VB7JRk9k0kg/0",
    "privilege": []
}


授权过程到此结束。

下面来个完整版的,新建一个oauth_complete.php文件。
将index.php中重定向url改为:$redirect_uri = urlencode("http://weiweiyi.duapp.com/oauth/oauth_complete.php");
oauth.complete.php

<?php
 
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$code = $_GET["code"];
$userinfo = getUserInfo($code);
echo $userinfo;
 
function getUserInfo($code)
{
    $appid = "wx07fff9c79a410b69";
    $appsecret = "092c0c0c5bd62f66b76ad241612915fb";
 
    //根据code获得access_token
    $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
        ."appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
    $access_token_json = Utils::https_request($access_token_url);
    $access_token_array = json_decode($access_token_json, true);
    //access_token
    $access_token = $access_token_array["access_token"];
    //openid
    $openid = $access_token_array["openid"];
 
    //根据access_token和openid获得用户信息
    $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?"
        ."access_token=$access_token&openid=$openid&lang=zh_CN ";
    $userinfo_json = Utils::https_request($userinfo_url);
    return $userinfo_json;
}


注意:获取的code只能使用一次,超过一次会报40163的错误,这时会获取不到access_token。
如果不是官方微信网页,会弹出以下界面:

其实在这之前已经发送了一次请求,点击“继续访问”实际上相当于第二次访问该页面,所以code也是第二次使用,会报错。

猜你喜欢

转载自blog.csdn.net/weixin_42980713/article/details/86562011
今日推荐