C # Development of WeChat Portal and application (i) How to use the Micro-communication interface

Source: Internet
Author: User
This article mainly for you to introduce the C # development portal and application of the first, the use of interfaces, with a certain reference value, interested in small partners can refer to

Application in full swing, many companies want to catch Information Express, this is a business opportunity, is also a technical direction, therefore, the research under the study of the related development, also become one of the important things of the daily plan. This series of articles hope that from a gradual point of view, a comprehensive introduction of the relevant development process and related experience summarized, I hope to give you a look at the relevant development history. This essay focuses on the preparation of the development process and the introduction of some initial work.

During the week before writing this article, I mainly refer to some introductory articles and the relevant interface description of the public platform, and combined with C # code development, organized the portal interface of their own company, to achieve the initial user interaction and information display work, with the further development of work, more and more functions may join, and want to extend the interface from the perspective of application, so as to achieve my technical exploration and understanding of the interface process.

1. Account number

To develop the platform API to use, you need to go to the public platform to register, have a service number or subscription number, service number mainly facing enterprises and organizations, the subscription number is mainly for organizations and individuals, there are certain differences between them, according to different needs of their own account can be applied.

In order to use some advanced interfaces, you may need to have a service number and advanced authentication. Account registration process, you need to download an application form, print and cover the official seal, in addition to the applicant to take the photo ID (a bit weird, hehe), and then uploaded to the server for review, General will soon be able to obtain the reply.

I in the name of the company to apply for service number, account registration, will display your relevant information in the main interface, in addition to you to apply for a two-dimensional code, scanning QR code can enter the company's Attention Confirmation dialog box, very convenient. The following is my application after the company account QR Code, you can use the scan directly.

2. Menu definition

There are two ways to define the menu, one is the editing mode, the other is the development mode, the two are mutually exclusive, that is, once we adopt the development mode, we cannot use the editing mode, and vice versa. Edit the menu, in fact, can also be managed, but not support, feel very uncomfortable.

In general, if we have just applied for a number, you can test it using the Edit menu, and then edit some menus to try it out. Although the update within 24 hours, but generally fast, the fastest possible two minutes to update the feeling is good.

Using the developer mode, you need to place a page link on the server based on the requirements, the use of C # developed, you can use the ***.ashx naming method, using ASP. NET General handler, no need to use normal pages.

Using the Development Mode menu, which can invoke the API for menu creation, we need to know that there are several parameters that are important to the calling API (there are a lot of API calls), so when the development mode is open, the parameters are listed as follows.

3, link processing of access

It says, you apply for development mode to the menu or to other API calls, you need to pass the test of access, that is, confirm that you fill in the link exists and can pass the callback test. Provides a PHP page processing example, if we are developed in C #, you can search to get the answer, my way is as follows.

Create a generic handler, and then add a processing logic in its processing page, if the content is non-post, that is, the get test, you need to add some processing logic, you can send it back to your content, if it is post, is the server to interface message request operation, described later.


<summary>//interface. An entry that receives and processes information uniformly. </summary> public class Wxapi:ihttphandler {public void ProcessRequest (HttpContext context) {  string post String = string. Empty;  if (HttpContext.Current.Request.HttpMethod.ToUpper () = = "POST")  {  using (Stream stream = HttpContext.Current.Request.InputStream)  {   byte[] postbytes = new Byte[stream. Length];   Stream. Read (postbytes, 0, (Int32) stream. Length);   poststring = Encoding.UTF8.GetString (postbytes);  }  if (!string. IsNullOrEmpty (poststring))  {   Execute (poststring);  }  }  else  {  Auth ();//test on Access  }}

In general, the AUTH function is to obtain the relevant parameters and then process them back to the server.


String token = "* * *";//When you apply, fill in the tokenstring echostring = httpcontext.current.request.querystring["Echostr"];string Signature = httpcontext.current.request.querystring["signature"];string timestamp = httpcontext.current.request.querystring["Timestamp"];string nonce = httpcontext.current.request.querystring[" Nonce "];

The complete author function code is shown below, where I have further extracted the business logic into a new class that facilitates the management of the business logic.


<summary>///To be the first step for developers to verify and respond to server data///</summary> private void Auth () {  string token = Configurationm Anager. appsettings["Weixintoken"];//Gets the token if (string) from the configuration file  . IsNullOrEmpty (token))  {  logtexthelper.error (string. Format ("Weixintoken configuration item is not configured! "));  }  String echostring = httpcontext.current.request.querystring["Echostr"];  String signature = httpcontext.current.request.querystring["signature"];  string timestamp = httpcontext.current.request.querystring["timestamp"];  String nonce = httpcontext.current.request.querystring["nonce"];  if (new Basicapi (). Checksignature (token, signature, timestamp, nonce))  {  if (!string. IsNullOrEmpty (echostring))  {   HttpContext.Current.Response.Write (echostring);   HttpContext.Current.Response.End ();  }  } }

Instead of signing the parameter and returning the Operation Checksignature, the code looks like this


<summary>///Authentication signature///</summary> public bool Checksignature (string token, string signature, String timest AMP, string nonce) {  string[] arrtmp = {token, timestamp, nonce};  Array.Sort (arrtmp);  String tmpstr = String. Join ("", arrtmp);  Tmpstr = FormsAuthentication.HashPasswordForStoringInConfigFile (tmpstr, "SHA1");  Tmpstr = Tmpstr.tolower ();  if (tmpstr = = signature)  {  return true;  }  else  {  return false;}  }

4. Create a menu using the development method

Once you pass the certification, it will let you invoke its API in development mode, and you can create your own menu.

The way you create a menu, you can enter into his API processing interface through the following location.

After entering, you will find that the processing of a lot of information, categorized into different categories inside.

In fact, the first thing we want to do now is how to look at it, using code to invoke the Create menu and go to the API Debug interface of the menu.

You will find that you need to enter a access_token, this is a session authentication, so you need to go to the interface to find out how to create it. The two red sections in the following figure are the two key parameters that prompted us to "developer credentials" when we started.

With this, you can create a menu based on the access_token you get, and according to the menu definition, it is divided into several categories, which can be divided into URL mode (View) and event mode (Click).

Click: After the user clicks the Click type button, the server pushes the message type to the developer via the message interface (refer to the Message Interface Guide), and with the key value entered by the developer in the button, the developer can interact with the user through a custom key value;
View: After the user clicks on the View Type button, the client will open the URL value (ie link) that the developer fills in the button to reach the goal of opening the webpage, and it is recommended to combine with the Web page authorization Access user's basic information interface to obtain the user's log in personal information.

5. The menu case I created

At the beginning of the essay, I published a QR code, once I use sweep, follow the service number, then you can see my own created menu. The main menu is usually up to three columns, and each main menu can have submenus, and their text is limited.

Let's take a look at my company's portal menu and look cool.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.