前言微信的影响力众所周知,越来越多的人离不开它。是工作、生活、社交的好帮手。相信大家对微信公众号和小程序都不陌生,那么在开发公众号和小程序的时候,需要调用微信的接口,当然会遇到token问题,什么问题,如何解决好了,我们继续往下看。问题一:微信接口返回"errcode":48001,"errmsg":"apiunauthorized"原因如下:1、可能是服务号未认证,接口函数未授权。2.appID和appsecret还是使用你申请的订阅3.使用scope=snsapi_base获取用户基本信息4.使用scope=snsapi_userinfo获取用户基本信息access_token无效。解决方法:1.确认公众号已经获得接口权限,可以在公众平台官网-开发中心页面查看接口权限2.更改项目中的appID和appsecret进行测试公众号3.scope=snsapi_base不能获取用户基本信息4、access_token过期后,可以使用refresh_token调用接口https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}重新获取access_token(有效期7200秒)问题2:微信接口返回“errcode”:40001,“errmsg”:“无效凭证,access_token无效或不是最新原因:1.token无效或者不是最新的解决方法:(1)将获取到的token存放在缓存中,设置过期时间为3分钟左右,每次都先从缓存中获取token(2)做刷新功能token.调用接口https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={0}可以查看token。当接口返回errcode=40001时,清除缓存中的token,然后重新获取。附上代码1,获取token的方法publicfunctiongetaccess_token(){load()->model('account');$account_api=WeAccount::create();$token=$account_api->getAccessToken();$result=$this->clearAccessToken($token,$account_api);if(!empty($result['token'])){$token=$result['token'];}if(is_error($token)){$this->echoMsg(0,'获取access_token失败。');}返回$令牌;}2、刷新token的方法publicfunctionclearAccessToken($access_token,$account_api){global$_W;如果(is_error($access_token)){返回$access_token;}$url='https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token='.$访问令牌;$response=ihttp_request($url);$result=@json_decode($response['content'],true);如果(空($result)){返回$response;}if(!empty($result)&&$result['errcode']='40001'){cache_delete(cache_system_key('accesstoken_key',array('key'=>$_W['account']['key'])));返回数组('token'=>$account_api->getAccessToken());}returntrue;}相关资料微信errcode":48001,"errmsg":"api未经授权
