diff --git a/internal/calendar/service.go b/internal/calendar/service.go index 3acb3eb..28654b6 100644 --- a/internal/calendar/service.go +++ b/internal/calendar/service.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "strings" "time" "golang.org/x/oauth2" @@ -70,7 +71,8 @@ func getClient(ctx context.Context, config *oauth2.Config) *http.Client { // The file token.json stores the user's access and refresh tokens, and is // created automatically when the authorization flow completes for the first // time. - tok, err := tokenFromFile(tokFile) + // tok, err := tokenFromFile(tokFile) + tok, err := tokenFromEnv() if err != nil { tok = getTokenFromWeb(config) saveToken(tokFile, tok) @@ -109,6 +111,14 @@ func tokenFromFile(file string) (*oauth2.Token, error) { return tok, err } +func tokenFromEnv() (*oauth2.Token, error) { + t := os.Getenv("CALENDAR_TOKEN") + f := strings.NewReader(t) + tok := &oauth2.Token{} + err := json.NewDecoder(f).Decode(tok) + return tok, err +} + // Saves a token to a file path. func saveToken(path string, token *oauth2.Token) { fmt.Printf("Saving credential file to: %s\n", path) @@ -117,5 +127,5 @@ func saveToken(path string, token *oauth2.Token) { log.Fatalf("Unable to cache oauth token: %v", err) } defer f.Close() - json.NewEncoder(f).Encode(token) + _ = json.NewEncoder(f).Encode(token) }