-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (67 loc) · 2.02 KB
/
main.go
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
73
74
75
76
77
78
79
80
package main
import (
"errors"
"fmt"
"io"
"os"
"github.com/dghubble/oauth1"
"github.com/sirupsen/logrus"
)
func main() {
oauth()
}
func oauth() {
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
log.SetFormatter(&logrus.JSONFormatter{})
consKey := os.Getenv("ETRADE_SANDBOX_API_KEY")
if consKey == "" {
log.Fatal(errors.New("missing env variable"))
}
consSecret := os.Getenv("ETRADE_SANDBOX_API_SECRET")
if consSecret == "" {
log.Fatal(errors.New("missing env variable"))
}
config := oauth1.Config{
ConsumerKey: consKey,
ConsumerSecret: consSecret,
CallbackURL: "oob",
Endpoint: oauth1.Endpoint{
RequestTokenURL: "https://api.etrade.com/oauth/request_token",
AuthorizeURL: "https://us.etrade.com/e/t/etws/authorize?key={}&token={}",
AccessTokenURL: "https://api.etrade.com/oauth/access_token",
},
}
requestToken, requestSecret, err := config.RequestToken()
if err != nil {
log.Fatal(err)
}
authorizationURL, err := config.AuthorizationURL(requestToken)
if err != nil {
log.Fatal(err)
} else {
fmt.Printf("open this url in your browser: %s\n\n", authorizationURL.String())
}
fmt.Printf("Choose whether to grant the application access.\nPaste " +
"the oauth_verifier parameter (excluding trailing #_=_) from the " +
"address bar: ")
var verifier string
_, err = fmt.Scanf("%s", &verifier)
accessToken, accessSecret, err := config.AccessToken(requestToken, requestSecret, verifier)
if err != nil {
log.Fatal("\n", err)
}
token := oauth1.NewToken(accessToken, accessSecret)
// Now that we have the token, create a new client that includes that access token so its requests are automatically processed
client := config.Client(oauth1.NoContext, token)
// Test application's access to account information & market data
path := "https://apisb.etrade.com/v1/market/quote/{AAPL}"
res, err := client.Get(path)
if err != nil {
fmt.Println("\n", err)
os.Exit(1)
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Printf("\nRaw Response Body:\n%v\n", string(body))
}