Implementing WeChat Official Account Integration

WeChat's developer documentation is available at https://mp.wiexin.qq.com/wiki/home/index.html


Integration Process Overview

To integrate with the WeChat Official Account platform, developers must follow these steps:

  1. Configure server settings
  2. Validate the server URL
  3. Implement business logic based on the API documentation

Using the Sandbox Environment

Access the testing platform at http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

Server URL Validation

After submitting configuration details, WeChat's servers will send a GET request to the specified URL with four parameters. Developers must verify the request's authenticity by checking the signature. If valid, return the echostr parameter unchanged to complete the integration.

Validation procedure:

  1. Sort the token, timestamp, and nonce parameters alphabetically
  2. Concatenate the three strings and compute the SHA-1 hash
  3. Compare the resulting hash with the signature parameter to verify the request's origin

Flask Application Implementation

Create a file named wechat_integration.py:

# -*- coding: utf-8 -*-
from flask import Flask, request
import hashlib

app = Flask(__name__)
SERVER_TOKEN = 'your_token_here'

@app.route('/wechat_endpoint')
def handle_wechat_verification():
    params = request.args
    
    received_signature = params.get('signature', '')
    nonce_value = params.get('nonce', '')
    time_stamp = params.get('timestamp', '')
    echo_string = params.get('echostr', '')
    
    verification_data = [SERVER_TOKEN, nonce_value, time_stamp]
    verification_data.sort()
    combined_string = ''.join(verification_data)
    
    computed_hash = hashlib.sha1(combined_string.encode()).hexdigest()
    
    if computed_hash == received_signature:
        return echo_string
    else:
        return 'Verification Failed', 403

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8016)

Deployment to Server

Transfer the application file to the server:

scp wechat_integration.py username@server_ip:~/

Establish a remote connection:

ssh username@server_ip

Activate the virtual environment:

workon flask_env

Verify file transfer:

ls -la

Configuration in Test Platform

Access the testing platform and configure:

  • URL: http://your_server_ip/wechat_endpoint
  • Token: your_token_here

Launch Application

Execute the Flask application on the server:

python wechat_integration.py

Submit the configuration in the test platform. Successful validation confirms proper integration.

Tags: WeChat API Flask Server Integration Webhook Verification Python Development

Posted on Tue, 15 Sep 2026 16:43:27 +0000 by magi