无法使用Golang获取Box API的访问令牌

I am not able to get access token for box api using golang.

Here's my code:

// box project main.go

package main

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

type accessinfo struct {access_token  string expires_in   int64 token_type    string refresh_token string}

var accessobj accessinfo

func try(w http.ResponseWriter, r *http.Request) {io.WriteString(w, "hello,world!
")if r.Method == "GET" {w.Header().Set("Content-Type", "text/plain")w.Write([]byte("This is an example.
"))code := r.FormValue("code")fmt.Println("gvshnbc")fmt.Println(code)authToken(code)dat, err := ioutil.ReadAll(r.Body)if dat == nil {log.Print("no data")} log.Print(string(dat))if err != nil {log.Print("no error")}}}func authToken(code string) {apiUrl := "https://app.box.com/api"///resource := "/oauth2/token"    data := url.Values{    data.Set("grant_type", "authorization_code")data.Add("code", code)fmt.Println(code) data.Add("client_id", "rnk5pqyahzrkf6bxwtc79rcief8u76p6")//data.Add("redirect_uri", "http://localhost:8089")data.Add("client_secret", "7xbeJvi76oc0IcHmfcUzZZPP9b0jVbDs")//data.Add("state", "authenticated") //fmt.Println(data)    u, _ := url.ParseRequestURI(apiUrl)//    u.Path = resource
fmt.Println(u)    urlStr := fmt.Sprintf("%v", u)    fmt.Println(urlStr)    client := &http.Client{}    //fmt.Println(client)    r, err := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))    if err != nil {        panic(err)
//break    }    //r.Header.Add("Content-Type", "application/x-www-form-urlencoded")    //r.Header.Add("Content-Length", "9")fmt.Println(r)    if err != nil {        fmt.Println(err)    }
    //fmt.Println(bytes.NewBufferString(data.Encode()))//fmt.Println(data.Encode())//fmt.Println(r)resp, err := client.Do(r)    if err != nil {        fmt.Println(err)    }    //fmt.Println(resp)    re, err := ioutil.ReadAll(resp.Body)    if err != nil {        fmt.Println("error")        fmt.Println(string(re))    }a := json.Unmarshal(re, &accessobj)fmt.Println(a)}

First off, you might want to remove your ClientID and ClientSecret from the code you posted.

Second, you'd probably be better off using an OAuth2 implementation that already exists code.google.com/p/goauth2/oauth is pretty good).

As to why your code doesn't work, the code you pasted isn't complete go code (it's missing a couple closing curly braces - data := url.Values{ is unclosed). Also, you should define a main function - without it your go code isn't going to run (unless there is other code using this code, that wasn't pasted above). If you want an example of go code that works to get a Box API token (shameless plug here), I wrote a bunch of stuff for a preliminary Box Client in go (I just never got around to finishing it). This little main will help you get a token: https://github.com/ttacon/box/blob/master/boxtoken/boxtoken.go.

To run it just cd into the directory it is in and run it with:

go run boxtoken.go -cid=YOUR_CLIENT_ID -csec=YOUR_CLIENT_SECRET

Then just go to http://localhost:8080/ and follow the page.

Hope that helps!