golang 清博数据
2019-12-17 本文已影响0人
王哈哈就很棒
accessToken.go
package main
import (
"crypto/md5"
"encoding/base64"
"fmt"
"io"
"net/url"
"sort"
"strings"
)
// 生成签名
func generateSign(params url.Values, app_secret string) (sign string){
// 1.取出所有的key
var keys []string
for k := range params{
keys = append(keys, k)
}
// 对字符串切片排序
sort.Strings(keys)
// 拼接字符串, 首尾加上下划线_
var stringA string
for _, k := range keys{
stringA = strings.Join([]string{stringA, fmt.Sprintf("%v%v", k, params[k][0] )}, "")
}
// 首尾加上下划线_, 再加上app_secret
stringA = fmt.Sprintf("%v_%v_%v", app_secret , stringA, app_secret)
// md5后得到签名sign
h := md5.New()
io.WriteString(h, stringA)
sign = fmt.Sprintf("%x", h.Sum(nil))
return
}
// 生成access-token
func GenerateAccessToken(params url.Values, router, app_key, app_secret string) (access_token string) {
// 生成签名
sign := generateSign(params, app_secret)
var param string = fmt.Sprintf("%v:%v:%v", app_key, sign, router)
// base64加密
access_token = base64.StdEncoding.EncodeToString([]byte(param))
return
}
main.go
package main
import (
"fmt"
"gopkg.in/ini.v1"
"io/ioutil"
"log"
"net/http"
"net/url"
)
var App_key string
var App_secret string
// 读取配置文件
func readConfig()(app_key, app_secret string){
cfg, err := ini.Load("douyin/config.ini")
if err != nil {
panic(err)
}
app_key = cfg.Section("qingbo").Key("app_key").String()
app_secret = cfg.Section("qingbo").Key("app_secret").String()
return
}
func main() {
App_key, App_secret = readConfig()
res := getAccessDetails("110814649")
fmt.Println(res)
}
// 抖音账号详情
func getAccessDetails(douyin_code string) string {
router := "/account/douyin/attribute"
params := make(url.Values)
params.Add("douyin_code", douyin_code)
access_token := GenerateAccessToken(params, router, App_key, App_secret)
request, err := http.NewRequest(http.MethodGet, "http://databus.gsdata.cn:8888/api/service", nil)
getErr(err)
request.URL.RawQuery = params.Encode()
request.Header.Add("access-token", access_token)
resp, err := http.DefaultClient.Do(request)
getErr(err)
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
return string(data)
}
func getErr(err error){
if err != nil {
log.Println("-----------------------------------------------------")
log.Println(err)
log.Println("-----------------------------------------------------")
}
}