-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (104 loc) · 2.26 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
api "google.golang.org/api/oauth2/v2"
"google.golang.org/api/option"
)
func main() {
setUpCmd()
}
func setUpCmd() {
loginCmd := flag.NewFlagSet("login", flag.ExitOnError)
logoutCmd := flag.NewFlagSet("logout", flag.ExitOnError)
emailCmd := flag.NewFlagSet("email", flag.ExitOnError)
if len(os.Args) == 1 {
fmt.Println("This command has login, logout, email commands. Please use them.")
return
}
switch os.Args[1] {
case "login":
loginCmd.Parse(os.Args[2:])
login()
case "logout":
logoutCmd.Parse(os.Args[2:])
logout()
case "email":
emailCmd.Parse(os.Args[2:])
getEmail()
default:
flag.Usage()
}
}
func loadSecrets() (string, string, error) {
id := os.Getenv("CLIENT_ID")
secret := os.Getenv("CLIENT_SECRET")
if id == "" || secret == "" {
return "", "", errors.New("CLIENT_ID & CLIENT_SECRET are required in env")
}
return id, secret, nil
}
func login() {
id, secret, err := loadSecrets()
if err != nil {
log.Fatalln(err)
}
auth := NewOAuth(
id,
secret,
[]string{
"https://www.googleapis.com/auth/userinfo.email",
},
)
ctx := context.Background()
log.Println("Login with browser")
if err := auth.Authorize(ctx); err != nil {
log.Fatalln(err)
}
if err := auth.SaveToken(); err != nil {
log.Println(err)
}
log.Println("Get & save token")
}
func logout() {
auth := NewOAuth("", "", nil)
auth.Revoke()
}
func getEmail() {
id, secret, err := loadSecrets()
if err != nil {
log.Fatalln(err)
}
auth := NewOAuth(id, secret, nil)
if err := auth.LoadToken(); err != nil {
log.Println(err)
log.Fatalln("Please login")
}
ctx := context.Background()
client := auth.Client(ctx)
email, err := getMailAddress(ctx, client)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("Your email address is %s\n", email)
// NOTE: It is not good. See a comment in saveTokenToFile().
if err := auth.SaveToken(); err != nil {
log.Println(err)
}
}
func getMailAddress(ctx context.Context, client *http.Client) (string, error) {
service, err := api.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return "", err
}
userInfo, err := service.Tokeninfo().Context(ctx).Do()
if err != nil {
return "", err
}
return userInfo.Email, nil
}