annotate calsync/gcalclient/service.go @ 58:6c7151126a0b

logging and refactor
author drewp@bigasterisk.com
date Thu, 05 Sep 2024 17:01:23 -0700
parents 5f7c393577e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
1 package gcalclient
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
2
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
3 import (
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
4 "context"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
5 "encoding/json"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
6 "fmt"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
7 "log"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
8 "net/http"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
9 "os"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
10
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
11 "golang.org/x/oauth2"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
12 "golang.org/x/oauth2/google"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
13 "google.golang.org/api/calendar/v3"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
14 "google.golang.org/api/option"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
15 )
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
16
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
17 // Retrieve a token, saves the token, then returns the generated client.
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
18 func getClient(config *oauth2.Config) *http.Client {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
19 // The file token.json stores the user's access and refresh tokens, and is
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
20 // created automatically when the authorization flow completes for the first
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
21 // time.
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
22 tokFile := "token.json"
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
23 tok, err := tokenFromFile(tokFile)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
24 if err != nil {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
25 tok = getTokenFromWeb(config)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
26 saveToken(tokFile, tok)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
27 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
28 return config.Client(context.Background(), tok)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
29 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
30
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
31 // Request a token from the web, then returns the retrieved token.
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
32 func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
33 authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
34 fmt.Printf("Go to the following link in your browser then type the "+
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
35 "authorization code: \n%v\n", authURL)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
36
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
37 var authCode string
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
38 if _, err := fmt.Scan(&authCode); err != nil {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
39 log.Fatalf("Unable to read authorization code: %v", err)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
40 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
41
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
42 tok, err := config.Exchange(context.TODO(), authCode)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
43 if err != nil {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
44 log.Fatalf("Unable to retrieve token from web: %v", err)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
45 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
46 return tok
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
47 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
48
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
49 // Retrieves a token from a local file.
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
50 func tokenFromFile(file string) (*oauth2.Token, error) {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
51 f, err := os.Open(file)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
52 if err != nil {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
53 return nil, err
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
54 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
55 defer f.Close()
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
56 tok := &oauth2.Token{}
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
57 err = json.NewDecoder(f).Decode(tok)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
58 return tok, err
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
59 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
60
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
61 // Saves a token to a file path.
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
62 func saveToken(path string, token *oauth2.Token) {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
63 fmt.Printf("Saving credential file to: %s\n", path)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
64 f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
65 if err != nil {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
66 log.Fatalf("Unable to cache oauth token: %v", err)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
67 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
68 defer f.Close()
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
69 json.NewEncoder(f).Encode(token)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
70 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
71
58
6c7151126a0b logging and refactor
drewp@bigasterisk.com
parents: 52
diff changeset
72 func newService(ctx context.Context) (*calendar.Service, error) {
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
73 b, err := os.ReadFile("./credentials.json")
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
74 if err != nil {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
75 log.Fatalf("Unable to read client secret file: %v", err)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
76 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
77
52
5f7c393577e9 now gets sync updates (for 30 sec)
drewp@bigasterisk.com
parents: 49
diff changeset
78 // If modifying these scopes, delete your previously saved token.json.
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
79 config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
80 if err != nil {
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
81 log.Fatalf("Unable to parse client secret file to config: %v", err)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
82 }
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
83 client := getClient(config)
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
84
58
6c7151126a0b logging and refactor
drewp@bigasterisk.com
parents: 52
diff changeset
85 return calendar.NewService(ctx, option.WithHTTPClient(client))
49
2991c1166852 start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff changeset
86 }