微信公众号获取access

您所在的位置:网站首页 错误40001 微信公众号获取access

微信公众号获取access

2024-01-11 12:40| 来源: 网络整理| 查看: 265

1,有时候出现40001错误微信文档说是(AppSecret错误或者AppSecret不属于这个公众号,请开发者确认AppSecret的正确性)

在这儿跟我扯淡呢!

2,如果写成每次访问我服务器接口都去请求微信接口就不会出现40001错误了,AppSecret错误的话不可能有时候可以获取到。查了百度说是过期或者其他地方把他给刷新了。

然而我存在 Redis 7190秒过期了会自动获取,更不会在别的地方刷新了(因为每次去获取access_token我都看了Redis有没有这个人的access_token)。个人感觉是这个access_token使用次数太频繁

3,解决思路: (1)反正每次访问你返回的是用户信息json串,索性我就把json串存到redis里,每次请求看redis有没有这人的信息。

这样就会有一个取消了关注Redis还是关注状态,不过我这里没关系。

(2)以下是代码,tp5.1框架的哦

/** * 查看是否关注公众号 */ public function whether_follow() { $uid = input('uid'); if(!$uid) { $res = [ 'code' => 0, 'msg' => 'UID 不存在' ]; }else{ // 在用户登录的时候获取了用户openid,存到数据库了 $openId = Db::name('users')->where('us_id', $uid)->value('us_puopenid'); $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); // 查看redis是否存在该用户信息 if($redis->exists('wxUserInfo:'.$uid)){ $res = $redis->get('wxUserInfo:'.$uid); }else{ // 不存在获取用户信息 $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->AppID.'&secret='.$this->AppSecret; $access_token = json_decode(go_curl($url, 'GET'), true); $token = $access_token['access_token']; // 获取用户信息 $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$token.'&openid='.$openId.'&lang=zh_CN'; $res = go_curl($url, 'GET'); // 直接把用户信息json串存redis $redis->set('wxUserInfo:'.$uid, $res, 7190); } } // 数据已经是json return $res; }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3