. Net public account development-basic interface and. net public interface
Author: Wang Xianrong
This article describes the use of basic interfaces in public account development, including the following:
(1) obtain the license token (AccessToken );
(2) obtain the server address;
(3) upload and download multimedia files;
(4) Create and display a QR code;
(5) Long Chain to short link.
Open Source Project: http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount
Source Code address: http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/blob/master/xrwang.net/Example/BasicInterface.aspx.cs
Demo address: http://xrwang.net/Example/BasicInterface.aspx
The demo effect is as follows:
1. obtain the license token.
The AccessToken class encapsulates the related attributes and methods of the license token.
(Note: oauthaccesen en encapsulates methods related to the web page authorization token .)
Attributes:
Access_token -- token string
Expires_in -- Effective time (unit: seconds)
Static methods include:
Get -- Get license token
/// <Summary> /// obtain the license token /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void btnGetAccessToken_Click (object sender, eventArgs e) {string userName = lbPublicAccount. selectedValue; AccessToken token = AccessToken. get (userName); txtaccesen en. text = token! = Null? Token. access_token: "An error occurred while obtaining the license token. ";}Example of obtaining a license token
2. Obtain the server address
The ServerAddresses class encapsulates the attributes and methods for obtaining server addresses.
Attributes:
Ip_list -- server address Array
Static methods include:
Get -- Get server address
/// <Summary> /// obtain the server address /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void btnGetServerAddress_Click (object sender, eventArgs e) {ErrorMessage errorMessage; ServerAddresses addresses = ServerAddresses. get (out errorMessage); if (errorMessage. isSuccess & addresses. ip_list! = Null) {StringBuilder sb = new StringBuilder (); foreach (string ip in addresses. ip_list) sb. appendFormat ("{0},", ip); txtServerAddress. text = sb. toString ();} else txtServerAddress. text = string. format ("failed to get server address. {0} ", errorMessage );}Example of getting a server address
3. upload and download multimedia files
The MultiMediaHelper class encapsulates methods related to multimedia files.
Static methods include:
Upload -- Upload a multimedia file
Download -- Download a multimedia file
GetDownloadUrl -- get the address of the multimedia file
GetVideoMediaId -- get the ID of the Video Media in the group message
/// <Summary> /// upload a multimedia file /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void btnUpload_Click (object sender, eventArgs e) {string userName = lbPublicAccount. selectedValue; MultiMediaTypeEnum type = (MultiMediaTypeEnum) Enum. parse (typeof (MultiMediaTypeEnum), lbMultiMediaType. selectedValue); string filename = fileUpload. fileName; byte [] bytes = fileUpload. file Bytes; ErrorMessage errorMessage; MultiMediaUploadResult result = MultiMediaHelper. Upload (userName, type, filename, bytes, out errorMessage); if (errorMessage. IsSuccess & result! = Null) hlShowMultiMedia. navigateUrl = MultiMediaHelper. getDownloadUrl (AccessToken. get (userName ). access_token, result. mediaId); else hlShowMultiMedia. navigateUrl = string. format ("javascript: alert ('failed to upload the multimedia file. \ R \ n {0} '); ", errorMessage );}Upload a multimedia file and generate an example of a download link
4. Create and display a QR code
The QrCode class encapsulates attributes and methods related to the QR code.
Attributes:
Ticket -- the ticket of the QR code
Expire_seconds -- validity period of the QR code (unit: seconds)
Url-address after the QR code image is parsed
Static methods include:
Create -- Create a QR code. Different overload methods can Create different types of QR codes.
GetUrl -- get the address of the QR code Image
/// <Summary> /// create a QR code /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void btnCreateQrCode_Click (object sender, eventArgs e) {string userName = lbPublicAccount. selectedValue; string strSceneId = txtSceneId. text; QrCode qrcode = null; ErrorMessage errorMessage; if (cbIsTemple. checked) {int expireSeconds = int. parse (txtExpireSeconds. text); int sceneId; if (in T. tryParse (strSceneId, out sceneId) qrcode = QrCode. create (userName, expireSeconds, sceneId, out errorMessage); else errorMessage = new ErrorMessage (ErrorMessage. predictioncode, "the scenario value id must be an integer. ");} Else {int sceneId; if (int. tryParse (strSceneId, out sceneId) qrcode = QrCode. create (userName, sceneId, out errorMessage); else qrcode = QrCode. create (userName, strSceneId, out errorMessage);} if (errorMessage. isSuccess & qrcode! = Null) imgQrCode. ImageUrl = QrCode. GetUrl (qrcode. ticket); else imgQrCode. ImageUrl = "";}Example of creating and displaying a QR code
5. Long Chain to short link
The signed URL encapsulates the method for converting a persistent connection to a short link.
Static methods include:
Get -- convert a long link to a short link
/// <Summary> /// obtain the short link /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void btnGetShortUrl_Click (object sender, eventArgs e) {string userName = lbPublicAccount. selectedValue; ErrorMessage errorMessage; string Invalid URL = Invalid URL. get (userName, txtLongUrl. text, out errorMessage); if (errorMessage. isSuccess & string. isNullOrWhiteSpace (Invalid URL) txtShortUrl. T Ext = shortUrl; else txtw.url. Text = string. Format ("failed to get the short link. {0} ", errorMessage );}Example of long chain to short link
Thank you for reading this article and hope to help you.