当前位置: 首页 > 工具软件 > go-openid > 使用案例 >

微信获取openid写入文件golang版_golang获取微信openid写入文件

寿元白
2023-12-01
package main

import (
    "encoding/json"
    "github.com/bitly/go-simplejson"
    "io/ioutil"
    "log"
    "net/http"
    "os"
)

func main() {

    //获取access_token 根据自身情况添加appid secret
    appid, secret := "XXXXX", "XXXXX"
    r := getFile("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret)
    var dat map[string]string
    json.Unmarshal([]byte(r), &dat)

    //获取全部openid并写入文件
    openids := getFile("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + dat["access_token"])

    //过滤黑名单openid
    log.Println("请勿关闭,程序正在运行中…………")
    uopenids, _ := simplejson.NewJson(openids)
    s, _ := uopenids.Get("data").Get("openid").StringArray()
    var inputString string
    for _, v := range s {
        s1 := getFile("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + dat["access_token"] + "&openid=" + v)
        up, _ := simplejson.NewJson(s1)
        groupid, _ := up.Get("groupid").Int()
        if groupid != 4 {
            inputString += v + "\n"
        }
    }
    //写入文件
    defer openFiles("2015September5.txt", inputString)

}

func openFiles(name string, con string) {
    f, _ := os.OpenFile(name, os.O_APPEND|os.O_CREATE, 0666)
    f.WriteString(con)
    defer f.Close()
}

func getFile(url string) []byte {
    oid, _ := http.Get(url)
    defer oid.Body.Close()
    oids, _ := ioutil.ReadAll(oid.Body)
    return oids
}

 

转载于:https://www.cnblogs.com/hcjs/p/4803123.html

 类似资料: