Home > WeChat Applet > WeChat Development > .net WeChat public account development - basic interface

.net WeChat public account development - basic interface

高洛峰
Release: 2017-02-22 15:45:38
Original
1731 people have browsed it

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:

.net WeChat public account development - basic interface

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 : "获取许可令牌失败。";
    }
Copy after login

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);
    }
Copy after login

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(&#39;上传多媒体文件失败。\r\n{0}&#39;);", errorMessage);
    }

上传多媒体文件,并生成下载链接的示例
Copy after login

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 = "";
    }
Copy after login

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);
    }
Copy after login

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!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template