1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
package main
import (
"log"
"net/http"
"os"
"strconv"
"com.teddywing/new-house-on-the-block/vendor/_nuts/github.com/fabioberger/coinbase-go"
"com.teddywing/new-house-on-the-block/vendor/_nuts/github.com/go-martini/martini"
)
var o *coinbase.OAuth
func main() {
m := martini.New()
m.Use(martini.Logger())
m.Use(martini.Recovery())
m.Use(martini.Static("public"))
r := martini.NewRouter()
m.MapTo(r, (*martini.Routes)(nil))
m.Action(r.Handle)
// Instantiate OAuthService with the OAuth App Client Id & Secret from the Environment Variables
o, err := coinbase.OAuthService(os.Getenv("COINBASE_CLIENT_ID"), os.Getenv("COINBASE_CLIENT_SECRET"), "https://localhost:8443/tokens")
if err != nil {
panic(err)
return
}
// At https://localhost:8443/ we will display an "authorize" link
r.Get("/", func() string {
authorizeUrl := o.CreateAuthorizeUrl([]string{
"user",
"balance",
})
link := "<a href='" + authorizeUrl + "'>authorize</a>"
return link
})
// AuthorizeUrl redirects to https://localhost:8443/tokens with 'code' in its
// query params. If you dont have SSL enabled, replace 'https' with 'http'
// and reload the page. If successful, the user's balance will show
r.Get("/tokens", func(res http.ResponseWriter, req *http.Request) string {
// Get the tokens given the 'code' query param
tokens, err := o.NewTokensFromRequest(req) // Will use 'code' query param from req
if err != nil {
return err.Error()
}
// instantiate the OAuthClient
c := coinbase.OAuthClient(tokens)
amount, err := c.GetBalance()
if err != nil {
return err.Error()
}
return strconv.FormatFloat(amount, 'f', 6, 64)
})
// HTTP
go func() {
if err := http.ListenAndServe(":8080", m); err != nil {
log.Fatal(err)
}
}()
// HTTPS
// To generate a development cert and key, run the following from your *nix terminal:
// go run $(go env GOROOT)/src/pkg/crypto/tls/generate_cert.go --host="localhost"
if err := http.ListenAndServeTLS(":8443", "cert.pem", "key.pem", m); err != nil {
log.Fatal(err)
}
}
|