add calendar token

This commit is contained in:
Ruidy 2024-08-06 09:18:58 +02:00
parent 9765edc318
commit 7ebe0975ae
No known key found for this signature in database
GPG key ID: E00F51288CB857CC

View file

@ -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)
}