Using Python to realize WeChat official account fan migration

Keywords: MySQL Python JSON pip

Recently, official account official has been merged into one public official account for the company's business needs, that is to say, one official account (mainly fans) should be transferred to another public number. Two According to WeChat specification, the openid of the same user in different official account is different. Our business system records the openid of the user without exception. Therefore, the conversion of openid to two official account is involved. Fortunately, WeChat public platform provides a way to describe official account migration and API for calling.

Here we use Python to write a program, which is simple and fast. The main knowledge points are:

  • MySQL connector, that is, Python DB API specification
  • HTTP client library requests using
  • WeChat official account platform API

First of all, the new and old openid cross reference table is established.

CREATE TABLE change_openidlist(
    ori_openid varchar(100) NOT NULL,
    new_openid varchar(100) NOT NULL,
    CONSTRAINT crm_change_openidlist_pk PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci ;

If not, you need to install the following class libraries first.

pip install mysql-connector-python
pip install requests

Next, run the following python program to write the old and new openid cross reference data to change ﹣ openidlist, and then update other data tables according to the data of this table.
Other visible annotations, not detailed, of course, do not forget to replace appid and secret with their own official account.

# -*- coding: utf-8 -*-
import requests
import mysql.connector

def handle_data():
        token = get_access_token()
        conn = mysql.connector.connect(host='', port='3306', user='user', password='password', database='wx', use_unicode=True,autocommit=True);
        qcursor = conn.cursor(buffered=True)
        wcursor = conn.cursor()
        #Old official account openid
        qcursor.execute('select openid from wxmembers')
        size = 100
        while True:
            list = qcursor.fetchmany(size)
            if not list:
            changeopenid_list = get_changeopenid_list(list,token)
            wcursor.executemany('insert into change_openidlist (ori_openid,new_openid) values (%s, %s)',changeopenid_list)
    except mysql.connector.Error as e:
        print ('Error : {}'.format(e))
        print 'openid handle finished!'

def get_access_token():
    new_appid = '00000'
    new_secret = '11111'
    url = 'https://API. Weixin. QQ. COM / CGI bin / token? Grant? Type = client? Credential '? Grant? Type is a fixed value
    payload = {'appid': new_appid, 'secret': new_secret}
    r = requests.get(url,params = payload)
    response = r.json()
    return response['access_token']

def get_changeopenid_list(ori_openid_list,token):
    new_access_token = token
    ori_appid = '33333'
    url = ''+ new_access_token
    payload = {'to_appid': ori_appid, 'openid_list': ori_openid_list}

    r =,json = payload)
    response = r.json()
    result_list = response['result_list']

    openid_list = [[result['ori_openid'],result['new_openid']] for result in result_list if result['err_msg'] == 'ok']

    return openid_list

if __name__ == '__main__':

Posted by nitharsanke on Sun, 03 May 2020 18:14:45 -0700