mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 13:46:51 +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"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
"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
|
// The file token.json stores the user's access and refresh tokens, and is
|
||||||
// created automatically when the authorization flow completes for the first
|
// created automatically when the authorization flow completes for the first
|
||||||
// time.
|
// time.
|
||||||
tok, err := tokenFromFile(tokFile)
|
// tok, err := tokenFromFile(tokFile)
|
||||||
|
tok, err := tokenFromEnv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tok = getTokenFromWeb(config)
|
tok = getTokenFromWeb(config)
|
||||||
saveToken(tokFile, tok)
|
saveToken(tokFile, tok)
|
||||||
|
|
@ -109,6 +111,14 @@ func tokenFromFile(file string) (*oauth2.Token, error) {
|
||||||
return tok, err
|
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.
|
// Saves a token to a file path.
|
||||||
func saveToken(path string, token *oauth2.Token) {
|
func saveToken(path string, token *oauth2.Token) {
|
||||||
fmt.Printf("Saving credential file to: %s\n", path)
|
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)
|
log.Fatalf("Unable to cache oauth token: %v", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
json.NewEncoder(f).Encode(token)
|
_ = json.NewEncoder(f).Encode(token)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue