微信 Token配置 与微信网页授权操作

微信开发准备工作

1、申请公众号测试账号地址,先注册账号

http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

2、更改接口信息配置的Token与URL地址
这里写图片描述

注:该URL是直接指向到具体能访问到的地址。

3、设置JS安全域名与设置网页授权回调地址
这里写图片描述

功能列表 – 网页服务


这里写图片描述

4、开始接入微信验证Token

<?php
namespace Test;

class TestController extends Controller 
{
    public function tokenSignature()
    {
        $timestamp = $_GET['timestamp'];
        $nonce = $_GET['noonce'];
        $token = 'sxs-hd'; //该处的值应该和第二条的TOKEN值一样
        $signature = $_GET['signature'];
        $array = array($timestamp, $nonce, $token);
        sort($array);
        //排序之后的数据拼接好使用sha1加密
        $tmpstr = implode('', $array);
        $tmpstr = sha1($tmpstr);
        //将加密后的字符串与singature进行对比,判断是否来自微信请求
        if ($tmpstr == $signature) {
            exit($_GET['echostr']);
        }
    }
}

5、获取自身的access_token值

<?php
    public function get_access_token()
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='. self::APP_ID .'&secret=' . self::APP_SECRET;
        $token = file_get_contents($url);
        var_dump($token);

        //正常返回
        {
            "access_token":"ACCESS_TOKEN",
            "expires_in":7200
        }
        //失败返回
        {
            "errcode":40013,
            "errmsg":"invalid appid"
        }
    }

6、获取用户网页身份授权
- 第一步:用户同意授权,获取code值

<?php
$params = http_build_query([
        'appid' => '你的APPID',
        'redirect_uri => urlencode('https://mm.shaxiaoseng.com/Test/code'), //获取之后回调地址
        'response_type' => 'code',
        'scope' => 'snsapi_base',
        'state' => 123
]);
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . $params . '#wechat_redirect';
#把上面的url在微信客户端打开就能看到返回的code值
  • 第二步:通过code换取网页授权access_token
//根据第一步获取到的code并回调到本方法
public function code()
{
        #根据拿到的code值去访问用户的access_token令牌
        $params = http_build_query([
                'appid' => 'APPID',
                'secret' => 'APP_SECERT',
                'code' => $_GET['code'],
                'grant_type' => 'authorization_code'
        ]);
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' . $params;
        $result = file_get_contents($url);

        var_dump($result);

        //正确返回
        { 
                "access_token":"ACCESS_TOKEN",
                "expires_in":7200,
                "refresh_token":"REFRESH_TOKEN",
                "openid":"OPENID",
                "scope":"SCOPE" 
        }
}
  • 第三步:刷新access_token令牌
  • 第四步:拉取用户信息
  • 第五步:检验授权凭证(access_token)是否有效

其它的开发可以直接查看微信官方手册 [手册地址]

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值