获取用户列表

获取用户列表的接口说明,参考:

https://developers.weixin.qq.com/doc/offiaccount/User_Management/Getting_a_User_List.html

该接口返回的是用户的OpenID。

例如在命令窗口执行以下代码,可以显示所有用户OpenID:


Dim
url As String = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}"
Dim
hc As New HttpClient(CExp(url, Functions.Execute("GetAccessToken")))
Dim
jo As JObject = JObject.Parse(hc.GetData)
If
jo("errcode") Is Nothing Then
    Dim sb As New StringBuilder
    For Each jk As JToken  In jo("data")("openid")
       sb.AppendLine(jk)
    Next
    Messagebox.Show(sb.ToString)

Else

    MessageBox.Show(jo.ToString)

End
If

提示:如果你的公众号有非常多的用户,不能用MessageBox来显示用户列表,应该保存为一个文本文件。

根据接口说明可知,上述代码最多只能获取前1万个OpenID,如果你的公众号用户数超过1万,需要采用下面的代码才能获取所有OpenID:

Dim url As String = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid={1}"
Dim
hc As New HttpClient(CExp(url, Functions.Execute("GetAccessToken"),""))
Dim
sb As New StringBuilder
Dim
jo As JObject = JObject.Parse(hc.GetData)
Do

    If jo("errcode") Is Nothing Then
        Dim cnt As Integer = jo("count")
        If cnt > 0 Then
            For Each jk As JToken  In jo("data")("openid")
                sb.AppendLine(jk)
            Next
            hcNew HttpClient(CExp(url, Functions.Execute("GetAccessToken"),jo("next_openid").Tostring))    
            jo = JObject.Parse(hc.GetData)
        Else
            Exit Do
        End If
    Else
        MessageBox.Show(jo.ToString)
        Exit Do
    End If

Loop

FileSys
.WriteAllText("c:\data\openid.txt", sb.ToString, False)



 


本页地址:http://www.foxtable.com/mobilehelp/topics/0184.htm