This is a creation in Article, where the information may have evolved or changed.
1: You want to register a developer, create an application, fill out the basic information and then fill in the callback address
2: Select the admin console in the upper-right corner---Select other APIs in the lower-left corner--Select security Settings--Authorization callback page
3: Test Code
As follows:
Package Main
Import (
Self-lead package
)
Type Accesstoken struct {
Accesstoken string ' JSON: ' Access_token '
Expiresin int ' JSON: "expires_in" '
Sessionsecret string ' JSON: ' Session_secret '
SessionKey string ' JSON: ' Session_key '
Scope string ' JSON: ' Scope '
}
Type UserInfo struct {
UserID string ' JSON: ' userid '
UserName string ' JSON: ' UserName '
}
Func Main () {
r: = Mux. Newrouter ()
R.handlefunc ("/listview", ListView)
R.handlefunc ("/upload", upload)
R.handlefunc ("/down", down)
R.handlefunc ("/social/oauth/callback", Index)
http. Handle ("/", R)
ERR: = http. Listenandserve (": 5555", nil)
If err! = Nil {
Panic (ERR)
}
}
Type User struct {
Name string
}
Func ListView (w http. Responsewriter, R *http. Request) {
if R.method = = "GET" {
T, err: = template. Parsefiles ("./html/list.html")
If err! = Nil {
Panic (ERR)
}
Items: = Make ([]*user, 0)
U1: = &user{"U1"}
U2: = &user{"U2"}
Items = Append (items, U1)
Items = Append (items, U2)
L: = Make (map[string]interface{})
Images: = []string{}
Images = append (images, "A")
Images = append (images, "B")
Images = append (images, "C")
l["Images"] = items
l["AAAA"] = images
T.execute (W, l)
}
}
Func Index (w http. Responsewriter, R *http. Request) {
Code: = R.formvalue ("code")
Item, Err: = Getaccess_token (Code, "own key", "own", "Http://www.yingyutwo.com:5555/social/oauth/callback")
If err! = Nil {
Fmt. Println ("err=" +err. Error ())
Return
}
UserInfo, err: = GetUserInfo (item. Accesstoken)
If err! = Nil {
Fmt. Println ("err=" +err. Error ())
Return
}
Fmt. Printf ("item=% #v \ n", userinfo)
Fmt. PRINTLN ("OK Index")
}
Get Access--token
Func Getaccess_token (Code, CLIENT_ID, Client_secret, Redirect_uri string) (*accesstoken, error) {
URL: = "https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code=" +code+ "&client_id= "+client_id+" &client_secret= "+client_secret+" &redirect_uri= "+redirect_uri
RESP, err: = http. Get (URL)
If err! = Nil {
return nil, err
}
Defer resp. Body.close ()
Body, err: = Ioutil. ReadAll (resp. Body)
If err! = Nil {
return nil, err
}
Accesstoken: = &accesstoken{}
Err = json. Unmarshal (body, Accesstoken)
Return Accesstoken, err
}
Get current logged on user information
Func GetUserInfo (Access_token string) (*userinfo, error) {
URL: = "https://openapi.baidu.com/rest/2.0/passport/users/getInfo?access_token=" +access_token
RESP, err: = http. Get (URL)
If err! = Nil {
return nil, err
}
Defer resp. Body.close ()
Body, err: = Ioutil. ReadAll (resp. Body)
If err! = Nil {
return nil, err
}
User: = &userinfo{}
Err = json. Unmarshal (body, user)
return user, Err
}
Page list.html
<!doctype html>
<meta character= "Utf-8" >
<title>List</title>
<body>
<a id= "Login_baidu" stats= "Loginpage_baidu_link" href= "Https://openapi.baidu.com/oauth/2.0/authorize?response_ type=code&client_id= own &redirect_uri=http://www.yingyutwo.com:5555/social/oauth/callback "> Baidu </a >
</body>
Welcome to join the Go language group 280096871