This article mainly introduces. net public account development-basic interface this article describes the use of basic interfaces in public account development, including the following content:
(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:
Get -- Get license token
////// Obtain the license token /////////Protected void btnGetAccessToken_Click (object sender, EventArgs e) {string userName = lbPublicAccount. SelectedValue; AccessToken token = AccessToken. Get (userName); txtAccessToken. Text = token! = Null? Token. access_token: "An error occurred while obtaining the 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
////// Obtain the server address /////////Protected void reply (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 );}
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
////// Upload a multimedia file /////////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. fileBytes; 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
////// Create a QR code /////////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 (int. tryParse (strSceneId, out sceneId) qrcode = QrCode. create (userName, expireSeconds, sceneId, out erro RMessage); else errorMessage = new ErrorMessage (ErrorMessage. ExceptionCode, "the scene 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 = "";}
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
////// Obtain the short link /////////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. text = response URL; else txtresponse URL. text = string. format ("failed to get the short link. {0} ", errorMessage );}
Thank you for reading this article and hope to help you.
For more articles about. net public account development-basic interfaces, refer to PHP Chinese website!