.net WeChat public account development - basic interface
This article describes the use of basic interfaces in the development of WeChat public accounts, including the following:
(1) Obtain the permission token (AccessToken);
(2) Obtain the WeChat server address;
(3) Upload and download multimedia files;
(4) Create and display QR codes;
(5) Convert long links to short links.
Open source project address: http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount
Source code address of this article: http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount /blob/master/xrwang.net/Example/BasicInterface.aspx.cs
The demonstration address of this article: http://xrwang.net/Example/BasicInterface.aspx
The demonstration effect is as follows:
1 Obtain the permission token
The AccessToken class encapsulates the relevant properties and methods of the permission token.
(Note: OAuthAccessToken encapsulates the related methods of web page authorization token.)
The attributes are:
access_token——Token string
expires_in——Validity time (unit: seconds)
Static methods are:
Get——Get the permission token
/// <summary> /// 获取许可令牌 /// </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); txtAccessToken.Text = token != null ? token.access_token : "获取许可令牌失败。"; }
2 Get the WeChat server address
The ServerAddresses class encapsulates the properties and methods for getting the server address.
Attributes are:
ip_list——server address array
Static methods are:
Get——get server address
/// <summary> /// 获取微信服务器地址 /// </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("获取微信服务器地址失败。{0}", errorMessage); }
3 Upload and download multimedia files
MultiMediaHelper class encapsulation Methods related to multimedia files.
Static methods are:
Upload——Upload multimedia files
Download——Download multimedia files
GetDownloadUrl——Get the address of multimedia files
GetVideoMediaId——Get the video media ID in the message group
/// <summary> /// 上传多媒体文件 /// </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.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('上传多媒体文件失败。\r\n{0}');", errorMessage); } 上传多媒体文件,并生成下载链接的示例
4 Create and display QR code
The QrCode class encapsulates the properties and methods related to QR code.
Attributes are:
ticket——the ticket of the QR code
expire_seconds——the validity time of the QR code (unit: seconds)
url——the address after parsing the QR code image
Static methods are:
Create——Create QR code, different overload methods can create different types of QR codes
GetUrl——Get the address of the QR code image
/// <summary> /// 创建二维码 /// </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 (int.TryParse(strSceneId, out sceneId)) qrcode = QrCode.Create(userName, expireSeconds, sceneId, out errorMessage); else errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "场景值id必须为整数。"); } 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 Converting a long link to a short link
ShortUrl encapsulates the method of converting a long link to a short link.
Static methods are:
Get - Convert long links into short links
/// <summary> /// 获取短链接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnGetShortUrl_Click(object sender, EventArgs e) { string userName = lbPublicAccount.SelectedValue; ErrorMessage errorMessage; string shortUrl = ShortUrl.Get(userName, txtLongUrl.Text, out errorMessage); if (errorMessage.IsSuccess && string.IsNullOrWhiteSpace(shortUrl)) txtShortUrl.Text = shortUrl; else txtShortUrl.Text = string.Format("获取短链接失败。{0}", errorMessage); }
Thank you for reading this article, I hope it is helpful to you.
For more .net WeChat public account development - basic interface related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
