初次用golang在公众号中获取信息,记录一下
看了下文档,粗略的写了个demo,如下:
func HttpGet(c*gin.Context) {
var param GetType
if er:=c.Bind(¶m);er!=nil{
c.JSON(200,gin.H{
"satus":200,
"msg":0,
})
}
resp, err := http.Get("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+param.Appid+"&secret="+param.Secret+"&code="+param.Code+"&grant_type=authorization_code")
if err != nil {
c.JSON(200,gin.H{
"satus":200,
"msg":1,
})
}
/*resp, err := http.Get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxf0e81c3bee622d60&secret=maxyo829&code=081Dq0kI0AzB3i2PiZhI0iQFjI0Dq0k-&grant_type=authorization_code")
if err != nil {
c.JSON(200,gin.H{
"satus":200,
"msg":1,
})
}*/ defer resp.Body.Close()
body, e := ioutil.ReadAll(resp.Body)
if e != nil {
c.JSON(200,gin.H{
"satus":200,
"msg":2,
})
}
//var jsonBlob = []byte(`[{"Name": "Platypus", "Order": "Monotremata"}]`)
var str ="["+string(body)+"]"
var jsonBlob = []byte(str)
var msg []Get
fmt.Println(string(jsonBlob))
error:=json.Unmarshal(jsonBlob,&msg)
/*get:=Get{}
fmt.Println(string(body))
msg:=[]byte (string(body))
error:=json.Unmarshal(msg,&get)*//*
fmt.Println(get)*/
if error!=nil{
c.JSON(500,gin.H{
"satus":500,
"msg":3,
"body":string(body),
})
return
}
c.JSON(200,gin.H{
"satus":200,
"msg":msg,
})
}
这样就拿到了一些基本的信息啦。