WeChat public platform development: general interface description

高洛峰
Release: 2017-02-27 13:13:37
Original
2149 people have browsed it

1. Basic description

The "Common APIs" mentioned here are necessary verification functions for using a series of advanced functions of WeChat public accounts (applied to development mode).

We use the unique credentials of the WeChat backend to make a request to the general interface to get the access token (AccessToken), and then use the access token to use various advanced functions that require authentication, such as custom menus, access User information, single messages, group messages, etc.

At present, all service accounts and certified subscription accounts can be found in the "Developer Credentials" column under [Function]>[Advanced Functions]>[Development Mode] in the WeChat public account background. Find the two strings AppId and AppSecret. These two strings are the basis for obtaining the AccessToken, so they need to be kept strictly confidential. If there is any possibility of leakage, or due to security policy, you should use the "Reset" button on the side to reset the AppSecret. Generate it randomly (note that the AppId will not change), and modify the corresponding parameters in the program at the same time.

WeChat public platform development: general interface description

2. Interface usage

In Senparc.Weixin.MP SDK, the basic methods of the common interface are in the Senparc.Weixin.MP.CommonAPIs namespace Next:

WeChat public platform development: general interface description

The file description is as follows:

AccessTokenContainer.cs - an AccessToken container (helps to automatically update AccessToken, because each AccessToken has a validity period)

CommonApi.cs - Provides a common method to obtain AccessToken

CommonApi.Menu.cs - All interfaces for custom menus

CommonJsonSend.cs - Some encapsulation of common interface return types Processing

For detailed methods and descriptions of the above classes, please see the comments of the open source code: https://github.com/JeffreySu/WeiXinMPSDK/tree/master/Senparc.Weixin.MP/Senparc.Weixin.MP/ CommonAPIs

With AccessTokenContainer, we can directly obtain AccessToken like this:

if (!AccessTokenContainer.CheckRegistered(appId))//检查是否已经注册
{
    AccessTokenContainer.Register(appId, appSecret);//如果没有注册则进行注册
}
var result = AccessTokenContainer.GetAccessTokenResult(appId); //获取AccessToken结果
Copy after login

Of course, it can also be done more simply in one step:

var result = AccessTokenContainer.TryGetAccessToken(appId, appSecret);
Copy after login

The results obtained above include access_token and expires_in The two attributes store the AccessToken string and expiration time (seconds) respectively. If you use the AccessTokenContainer.TryGetAccessToken() method, you can completely ignore the existence of expires_in. If it expires, the system will automatically re-obtain it.


For more WeChat public platform development: general interface description related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template