用go golang获取access_token
ganmao 发布于2018-01 浏览:6012 回复:11
0
收藏
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

func main() {
	v := url.Values{}
	v.Set("grant_type", "client_credentials") //规定,无需修改
	v.Add("client_id", "YM1OvG...K") //根据自己申请填写
	v.Add("client_secret", "S926...Wq") //根据自己申请填写
	res, err := http.PostForm("https://aip.baidubce.com/oauth/2.0/token", v)
	robots, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s", robots)
}
收藏
点赞
0
个赞
共11条回复 最后由SKueYun回复于2019-03
#13SKueYun回复于2019-03

可以了

0
#12SKueYun回复于2019-03

显示The authorization grant type is not supported是怎么回事??

 

func auth( apikey, secretkey string ) (string, error) {
    client := &http.Client{}
    
    v := url.Values{}
    v.Set("grant_type", "client_credentials")
    v.Add("client_id", apikey) 
    v.Add("client_secret", secretkey)
    
req, err := http.NewRequest("GET", ACCESSTOKE_URL, bytes.NewBufferString(v.Encode()))
if err != nil {
        // handle error
        return "", err
}

    resp, err := client.Do(req)
    if err != nil{
        return "", err
    }

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
        return "", err
}

    fmt.Println(string(body))
    
    return string(body), nil
}
0
#11SKueYun回复于2019-03

显示The authorization grant type is not supported是怎么回事??

 

func auth( apikey, secretkey string ) (string, error) {
    client := &http.Client{}
    
    v := url.Values{}
    v.Set("grant_type", "client_credentials")
    v.Add("client_id", apikey)
    v.Add("client_secret", secretkey)
    
req, err := http.NewRequest("GET", ACCESSTOKE_URL, bytes.NewBufferString(v.Encode()))
if err != nil {
        // handle error
        return "", err
}

    resp, err := client.Do(req)
    if err != nil{
        return "", err
    }

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
        return "", err
}

    fmt.Println(string(body))
    
    return string(body), nil
}

0
#10SKueYun回复于2019-03

请问header里面的authorization是怎么签名?? 有没有代码分享一下????

0
#9SKueYun回复于2019-03

请问header里面的authorization是怎么签名?? 有没有代码分享一下????

0
#7ganmao回复于2018-01
//总是说图片格式不对。。。
func generalData() {
	cli := &http.Client{}
	accessToken := "24....874"
	url := "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + accessToken

	ff, _ := ioutil.ReadFile("z:\\mate10.png")
	str := base64.URLEncoding.EncodeToString(ff)
	str = "image=" + str
	fmt.Println(str)
	data := bytes.NewReader([]byte(str))

	req, err := http.NewRequest("POST", url, data)
	if err != nil {
		log.Fatal(err)
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	resp, err := cli.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	robots, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s", robots)
}
0
#6ganmao回复于2018-01
#4 ganmao回复
做一下记录

想做个go语言的SDK

0
#5ganmao回复于2018-01
// 通用接口URL调用
func general() {
	cli := &http.Client{}
	accessToken := "24...88874"
	url := "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + accessToken

	req, err := http.NewRequest("POST", url, strings.NewReader("url=http://www-file.huawei.com/-/media/CORPORATE/Images/home/big-banner/2017/mate10-text-big-banner-cn.png?la=zh"))
	if err != nil {
		log.Fatal(err)
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	resp, err := cli.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	robots, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s", robots)
}
0
#4ganmao回复于2018-01
#3 用户已被禁言回复
分享代码?

做一下记录

0
#3用户已被禁言回复于2018-01

分享代码?

1
#2ganmao回复于2018-01

获取client_id,client_sectet的方式见

http://ai.baidu.com/docs#/Begin/top

grant_type: 必须参数,固定为client_credentials;
client_id: 必须参数,应用的API Key;
client_secret: 必须参数,应用的Secret Key;

0
TOP
切换版块