Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of the General Interface for the Development of Wechat Public platform

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of the common interface for the development of Wechat public platform, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article. Now let the editor take you to know about it.

I. basic instructions

The "CommonAPIs" mentioned here is a necessary verification function (applied to the development mode) that uses a series of advanced features of the Wechat public account.

We use the unique credentials at the Wechat backend to send a request to the general interface, get an access token (AccessToken), and then use the access token to use various advanced functions that require authentication, such as customizing menus, obtaining user information, sending single messages, sending mass messages, and so on.

At present, all service numbers and certified Subscription account can find the strings AppId and AppSecret in the "developer credentials" column under "Features"-> "Advanced Features"-> "Development Mode" in the Wechat public account backend. These two strings are the basis for obtaining AccessToken, so they need to be kept strictly confidential, if it is found that there is a possibility of disclosure, or out of security policy. You should use the reset button on the side to randomly generate the AppSecret (note that the AppId does not change) and modify the corresponding parameters in the program at the same time.

II. Interface use

In Senparc.Weixin.MP SDK, the basic methods of common interfaces are under the Senparc.Weixin.MP.CommonAPIs namespace:

The document is described as follows:

AccessTokenContainer.cs-An AccessToken container (helps update AccessToken automatically, because each AccessToken has an expiration date)

CommonApi.cs-provides a common way to get AccessToken

CommonApi.Menu.cs-Custom menu all interfaces

CommonJsonSend.cs-some encapsulation of the return type of the generic interface

For detailed methods and instructions on the above classes, you can take a look at the comments on the source code: https://github.com/JeffreySu/WeiXinMPSDK/tree/master/Senparc.Weixin.MP/Senparc.Weixin.MP/CommonAPIs

With AccessTokenContainer, we can get the AccessToken directly like this:

If (! AccessTokenContainer.CheckRegistered (appId)) / / check whether {AccessTokenContainer.Register (appId, appSecret) has been registered; / / if not, register} var result = AccessTokenContainer.GetAccessTokenResult (appId); / / get the AccessToken result

Of course, it can also be easier to achieve in one step:

Var result = AccessTokenContainer.TryGetAccessToken (appId, appSecret)

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

Thank you for reading this article carefully. I hope the article "sample Analysis of the Common Interface for the Development of Wechat Public platform" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report