微信小程序中獲取微信公衆號文章

微信小程序獲取微信公衆號文章

1. 獲取access_token

  • 微信公共號的APPSECRET 這個密鑰要啓用纔可以看到。

  • 在開啓APPSECRET密鑰之後,會出現一個添加ip白名單。如果不添加,會報錯:

    { errcode: 40164,
    errmsg: 'invalid ip 117.100.47.169 ipv6 ::ffff:117.100.47.169, not in whitelist hint: [39lrcA01394100]' }
    
  • 獲取微信的access_token。最好在自己服務器後臺寫,因爲要輸入AppID和AppSecret;
    接口調用請求說明: https請求方式: GET

    https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
    

2. 拿到access_token後,在小程序中獲取微信公衆號的素材列表:

getArtLists(accessToken){
   wx.showLoading({
        title: '加載中',
      })
      wx.request({
        url: 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=' + accessToken
        data: {
         "type": 'news',
          "offset": 0,
          "count": 20
        },
        method:'POST',
        header: {
          'content-type': 'application/json'
        },
        success(res) {
        console.log('微信素材列表',res)
        },
        fail(res){
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        },
        complete(){
          wx.hideLoading()
        }
      })
      }

調用接口地址:

https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token= '添加自己的access_token'

官方文檔鏈接

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章