mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
add calendar token
This commit is contained in:
parent
9765edc318
commit
7ebe0975ae
1 changed files with 12 additions and 2 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue