• 配置IP 白名單

    在公眾號(hào)后臺(tái)的「設(shè)置與開(kāi)發(fā)」-「基本配置」,“IP白名單”設(shè)置即可,把你的ip(執(zhí)行api調(diào)用的機(jī)器ip)添加進(jìn)去。沒(méi)有添加,發(fā)起請(qǐng)求會(huì) “errcode: 40164,errmsg: invalid ip”。

  • 接口封裝發(fā)布文章

    接口封裝發(fā)布文章一共就四步:獲取token、上傳封面、保持草稿、發(fā)布。

    1 獲取token

    幾乎所有的接口都用token做身份認(rèn)證的,接口功能調(diào)用的第一步。

    參考文檔:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html

    image

    調(diào)用接口為:

    https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={yourappid}&secret={yoursecret}

    用postman來(lái)做請(qǐng)求測(cè)試:

    image

    代碼:(去除了日志信息,下同)

    image

    2 新增一個(gè)永久素材

    主要是后面的操作,需要一個(gè)圖片作為封面,所以需要提前上傳一個(gè)圖片資源,對(duì)應(yīng)的接口就是“新增永久素材”了

    參考文檔:https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html

    image

    調(diào)用接口:

    https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={access_token}&type=image

    代碼:

    image

    提示:如果多次發(fā)布文章,使用同樣的封面,則可以先調(diào)用這個(gè)接口,把media_id 保存下來(lái),之后這步就不是必要的了,后面新建草稿時(shí)候,直接用寫定的media_id就可。

    3 新建草稿

    參考文檔:https://developers.weixin.qq.com/doc/offiaccount/Draft_Box/Add_draft.html

    image

    調(diào)用接口:

    https://api.weixin.qq.com/cgi-bin/draft/add?access_token={access_token}

    填上我們關(guān)注的必填選就可,需要填寫的不多。

    代碼:

    image

    注意:如果發(fā)布后,內(nèi)容有亂碼,如:

    image

    那是編碼問(wèn)題,構(gòu)造json 數(shù)據(jù)時(shí)候,ensure_ascii 用false。上例是python代碼,如果json.dumps(drafts)會(huì)出錯(cuò)。需要改為:json.dumps(drafts, ensure_ascii=False).encode(‘utf-8’) 。其他語(yǔ)言也有對(duì)應(yīng)的設(shè)置方法。

    4 發(fā)布接口

    最后一步就是發(fā)布了,這個(gè)接口最為簡(jiǎn)單。直接把前面的增加草稿返回的media_id 提交下就可。

    參考文檔:https://developers.weixin.qq.com/doc/offiaccount/Publish/Publish.html

    image

    調(diào)用接口:

    https://api.weixin.qq.com/cgi-bin/freepublish/submit?access_token={access_token}

    代碼:

    image

    5 其他

    這步不是必要的,不關(guān)注發(fā)布狀態(tài)的就可以無(wú)視。由于發(fā)布文章是異步的,同時(shí)發(fā)布后的文章可能還被內(nèi)容審核啥的,所以發(fā)布后的文章,可以通過(guò)publish_id 去請(qǐng)求“發(fā)布狀態(tài)輪詢接口”接口查詢狀態(tài)。

    整合發(fā)表功能基本函數(shù)封裝好之后,發(fā)表文章就是一步到位了:

    image

    特別提醒:現(xiàn)在公眾號(hào)是推薦制度,測(cè)試結(jié)果:通過(guò)api發(fā)布的文章,不會(huì)觸發(fā)微信系統(tǒng)推薦,也不會(huì)在主頁(yè)顯示,可用于菜單或者鏈接分享方式給用戶閱讀。

    完整代碼

    #-*- coding: UTF-8 -*-
    import json
    import os
    import requests
    def _str_of_json(content, key):
        res = json.loads(str(content, 'utf8'))
        if key not in res or res[key] == "":
            print(res)
            return False, ""
        return True, res[key]
    
    # 獲取token 返回(sucess, token)
    def get_token(appid, secret):
        url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" % (appid, secret)
        res = requests.get(url)
        return _str_of_json(res.content, "access_token")
    
    # 上傳圖片 返回(sucess, media_id)
    def upload_image(token, file_path):
        url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=image" % token
        with open(file=file_path, mode='rb') as fp:
            files = {'media': fp}
            resp = requests.post(url, files=files)
            return _str_of_json(resp.content, "media_id")
        return False, ""
    
    # 新建草稿 返回(sucess, media_id)
    def add_draft(token, cover_media_id, title, content):
        url = "https://api.weixin.qq.com/cgi-bin/draft/add?access_token=%s" % token
        article = {
            "title":title,
            "thumb_media_id":cover_media_id,
            "content":content,
            # "author":"author", # 非必填
            # "digest":"digest", # 非必填
        }
        drafts = {
            "articles":[
                article
            ]
        }
        headers = {'Content-Type': 'application/json'}
        # 直接post json.dumps(drafts) 會(huì)有亂碼,所以
        post_data = json.dumps(drafts, ensure_ascii=False).encode('utf-8')
        res = requests.post(url, data=post_data, headers=headers)
        return _str_of_json(res.content, "media_id")
    
    # 發(fā)布,返回(sucess, publish_id)
    def publish(token, draft_id):
        url = "https://api.weixin.qq.com/cgi-bin/freepublish/submit?access_token=%s" % token
        res = requests.post(url, json={"media_id":draft_id})
        return _str_of_json(res.content, "publish_id")
    
    def publish_article(appid, secret, title, content, cover_file_path):
        # 1 獲取token
        res, token = get_token(appid, secret)
        # 2 上傳封面
        res, cover_media_id = upload_image(token, cover_file_path)
        # 3 保存草稿
        ret, draft_id = add_draft(token, cover_media_id, title, content)
        # 4 發(fā)布
        ret, publish_id = publish(token, draft_id)
    
    if __name__ == '__main__':
        appid = "wx000000000000" # 你的 appid
        secret = "xxxxxxxxxxxxxxx" # 你的 secret
        file_path = "/Users/xxx/yyyy.jpg" # 你的文章封面圖片
        title = "你的文章標(biāo)題"
        content = "你的文章內(nèi)容"
        publish_article(appid, secret, title, content, file_path)

    原文轉(zhuǎn)載自:https://mp.weixin.qq.com/s/vu9Aep-QBwN0xu4i28W3UA

    上一篇:

    使用Python開(kāi)發(fā)1688商品詳情API接口

    下一篇:

    API接口安全—webservice、Swagger、WEBpack
    #你可能也喜歡這些API文章!

    我們有何不同?

    API服務(wù)商零注冊(cè)

    多API并行試用

    數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

    查看全部API→
    ??

    熱門場(chǎng)景實(shí)測(cè),選對(duì)API

    #AI文本生成大模型API

    對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

    25個(gè)渠道
    一鍵對(duì)比試用API 限時(shí)免費(fèi)

    #AI深度推理大模型API

    對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

    10個(gè)渠道
    一鍵對(duì)比試用API 限時(shí)免費(fèi)