微信公众号开发教程获取access_token
在微信公众号开发过程中,如果我们想要主动向微信服务器推送消息那么我们必须要有access_token。access_token 是公众号的唯一票据,当我们开发调用微信的各种接口时都需要使用access_token。access_token是类似于下面所展示的一个字符串。
wGOSbMw6iKvhIOcjCJTi_r6wXpOF_Vnih-TEvumx_1OXgnaZiRfy0g0hJ08ykRkc0bMAYTzKdZoAmZ32ROdmgo5_JQbgTcPth2bajjoRqfEeC_VRz53cl2_CLavkNkBTSKWcAEAMEQ
这个字符串比较长。所以说access_token的存储至少要保留512个字符空间。并且得到的这一个access_token并不是永久有效的,它的有效时间是2个小时。这个有效时间会附加在我们获取access_token的返回结果中,字段名称是expires_in。关于如何获取access_token我们在下面会讲到。下面我们先来看一下微信官方是如何说明access_token的使用及生成方式的。
1、为了保密appsecrect,第三方需要一个access_token获取和刷新的中控服务器。而其他业务逻辑服务器所使用的access_token均来自于该中控服务器,不应该各自去刷新,否则会造成access_token覆盖而影响业务;
2、目前access_token的有效期通过返回的expire_in来传达,目前是7200秒之内的值。中控服务器需要根据这个有效时间提前去刷新新access_token。在刷新过程中,中控服务器对外输出的依然是老access_token,此时公众平台后台会保证在刷新短时间内,新老access_token都可用,这保证了第三方业务的平滑过渡;
3、access_token的有效时间可能会在未来有调整,所以中控服务器不仅需要内部定时主动刷新,还需要提供被动刷新access_token的接口,这样便于业务服务器在API调用获知access_token已超时的情况下,可以触发access_token的刷新流程。
所以说,对于access_token我们应该定时去刷新获取。否则的话,重复获取将导致上次获取的access_token失效。
好,说了这么多,也了解到access_token的重要性,那么我们到底应该怎么来获取它呢?其实,获取access_token也挺简单的。微信为我们提供了http接口:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
注意:调用所有微信接口时均需使用https协议。
上面的接口需要我们发送三个参数:
grant_type 该参数在获取access_token时要填写client_credential
appid 这个参数在基本配置中可以获取,对于每一个开发者都有一个唯一的appid,作为第三方用户的唯一凭证
secret 这个参数的值是appsecret 同样对于每一个开发者都有一个appsecret,它是第三方用户唯一凭证密钥。和appid一样,appsecret也可以在基本配置中得到。
清楚了这三个参数,下面我们就开始来调用这个接口。因为是http接口,我们使用php语言来调用,所以需要用到curl。
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . self::appId . "&secret=" . self::appSecret;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
返回的结果是一个json串,该json串包含两个内容:一个是access_token,另一个是有效时间(expires_in)。
{"access_token":"gq84-toUzqn2mNX_ibmvMOIVm1TIKPD6mGLUfMNdCytpsLv6H-aCZgXMPkEt_D4Iqjjoe8w_qgo_9oli3dECpimhb-FUONTEJLarG6PGYSpPZSAb8rcwv4WR0BMwwc8ESCXiAGABLG","expires_in":7200}
同样,调用错误出现错误的话也会返回一个json串,该json串的内容根据错误原因的不同而有所不同。示例如下:
{"errcode":40013,"errmsg":"invalid appid"}
access_token使用案例——获取微信服务器ip地址
上面我们介绍了access_token的用途以及该如何获取access_token。这里我们介绍一个access_token的使用案例——如果公众号基于安全等考虑,我们需要获知微信服务器的IP地址列表,以便进行相关限制,可以通过相应接口获取微信服务器ip地址列表。
其接口如下
https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=ACCESS_TOKEN
我们看,这个接口只有一个参数——access_token 。下面我们来看如何使用
$url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$this->get_accesstoken();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
返回的结果同样也是一个json字符串。
{ "ip_list":["ip1"," ip2"] }
同样,该接口也可能返回调用错误。其错误原因多数是access_token无效或过期
{"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest hint: [w2KXsa0239vr29!]"}
好,上面就是对于access_token的用途及获取方式做的一个详细的介绍。
相关文章
WeChat public account development tutorial interface configuration and identity a
发布时间:2025/03/16 浏览次数:133 分类:NETWORK
-
To become a developer of WeChat official account, you must first have a server that can be accessed externally. The development of WeChat official account does not limit the use of any language. Here we use PHP as the development language.
WeChat public account development tutorial to obtain access_token
发布时间:2025/03/16 浏览次数:75 分类:NETWORK
-
During the development of WeChat official accounts, if we want to actively push messages to the WeChat server, we must have access_token. Access_token is the only ticket for the official account. When we develop and call various WeChat inte
WeChat public account receives messages and processes ordinary messages
发布时间:2025/03/16 浏览次数:153 分类:NETWORK
-
There are two situations when WeChat users interact with public accounts: one is when WeChat users send ordinary messages to public accounts; the other is when some operations of WeChat users cause the WeChat server to notify the URL filled
WeChat public account receiving message event message processing
发布时间:2025/03/16 浏览次数:80 分类:NETWORK
-
As we know, there are two types of messages generated by the interaction between WeChat users and public accounts: one is ordinary messages, which are introduced in detail in the article "Ordinary message processing for WeChat public accoun
OAuth2.0 - How to issue access tokens
发布时间:2025/03/17 浏览次数:135 分类:NETWORK
-
In the previous article, we introduced that OAuth2.0 is an authorization mechanism whose main purpose is to issue tokens between websites or applications that want to share resources. Before starting this article, we assume that you have un
微信公众号接收消息 事件消息处理
发布时间:2016/10/18 浏览次数:4683 分类:网络
-
事件消息分为关注/取消关注、扫描带参数的二维码、上报地理位置、自定义菜单、点击菜单拉取消息、点击菜单跳转链接共六种事件。
微信公众号接收消息 普通消息处理
发布时间:2016/10/14 浏览次数:2391 分类:网络
-
在微信用户和公众号产生交互的过程中会分为两种情况:一种是微信用户向公众号发送普通消息;另一种是微信用户的某些操作使得微信服务器通过事件推送的形式通知到开发者填写的
微信公众号开发教程 接口配置与身份验证
发布时间:2016/10/11 浏览次数:2667 分类:网络
-
要想成为微信公众号的开发者,首先必须有可以对外访问的服务器。微信公众号的开发不限制使用什么语言,这里我们使用PHP作为开发语言。