-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle.go
163 lines (137 loc) · 4.1 KB
/
google.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package goc
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
const TOKEN_FILE = "/token.json"
const CREDENTIALS_FILE = "/credentials.json"
func GetClient() (*calendar.Service, oauth2.TokenSource) {
config := getConfig()
tok := getToken(config)
ctx := context.Background()
source := config.TokenSource(ctx, tok)
client := oauth2.NewClient(ctx, source)
srv, err := calendar.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
log.Fatalf("unable to retrieve client: %v", err)
}
return srv, source
}
func getConfig() *oauth2.Config {
path := getSharedPath()
b := getCredentials(path)
// Need to delete token.json on a scope change
config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope, calendar.CalendarEventsScope)
if err != nil {
log.Fatalf("unable to parse client secret file to config: %v", err)
}
return config
}
func getToken(config *oauth2.Config) *oauth2.Token {
path := getSharedPath()
tokFile := path + TOKEN_FILE
tok := getTokenFromFile(tokFile)
if tok == nil {
tok = getTokenFromWeb(config)
saveToken(tokFile, tok)
fmt.Println("Run setup again to select calendar")
os.Exit(0)
}
return tok
}
func updateToken(source oauth2.TokenSource) {
path := getSharedPath()
tokFile := path + TOKEN_FILE
tok := getTokenFromFile(tokFile)
if tok == nil {
log.Printf("unable to read token from file")
}
sourceToken, err := source.Token()
if err != nil {
log.Printf("unable to get token from source: %v", err)
return
}
if sourceToken == nil {
log.Println("source token is nil")
return
}
saveToken(tokFile, sourceToken)
}
func getCredentials(path string) []byte {
b, err := os.ReadFile(path + CREDENTIALS_FILE)
if err != nil {
writeCredentialsInstructionsAndExit(path)
}
return b
}
func writeCredentialsInstructionsAndExit(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.Mkdir(path, os.ModePerm); err != nil {
log.Fatal(err)
}
}
fmt.Println("Setup and download the Google credentials.json file here: https://console.cloud.google.com/apis/credentials")
fmt.Println("- Create a new project")
fmt.Println("- Setup OAuth consent screen")
fmt.Println("- Fill out all required fields")
fmt.Println("- For scopes, add `calendarlist.readonly` and `calendar.events`")
fmt.Println("- For test users, add your own email")
fmt.Println("- Set app in *production* to remove 7 day token limit")
fmt.Println("- Click on credentials and create credentials then select OAuth client ID")
fmt.Println(" - Set application type to Desktop app and follow the steps")
fmt.Println(" - Choose download JSON after creating the credential")
fmt.Println(" - Rename the file you downloaded to `credentials.json`")
fmt.Println(" - Move this file into `" + path + "`")
fmt.Println()
fmt.Println("- Run goc setup to continue")
os.Exit(0)
}
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the link below and follow the steps.\n\n%v\n\nPaste the 'code' value from the localhost-URL here: ", authURL)
var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
log.Fatalf("unable to scan input: %v", err)
}
tok, err := config.Exchange(context.Background(), authCode)
if err != nil {
log.Fatalf("unable to retrieve token from web: %v", err)
}
return tok
}
func getTokenFromFile(file string) *oauth2.Token {
f, err := os.Open(file)
if err != nil {
return nil
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
if err != nil {
return nil
}
return tok
}
func saveToken(path string, token *oauth2.Token) {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("unable to save oauth tokens: %v", err)
}
defer f.Close()
err = json.NewEncoder(f).Encode(token)
if err != nil {
log.Fatalf("unable to encode token: %v", err)
}
}
// ! not used
func deleteTokenFile() {
path := getSharedPath()
os.Remove(path + TOKEN_FILE)
}