From 84ee48d8b1060a14974ec811a0ee8000e3f1aa31 Mon Sep 17 00:00:00 2001 From: Andrea Spacca Date: Fri, 26 Oct 2018 23:28:47 +0200 Subject: [PATCH] save token in gdrive local config path (#170) --- README.md | 2 +- server/storage.go | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3d3f387..f7f8400 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ If you want to use TLS using your own certificates, set tls-listener to :443, fo Make sure your GOPATH is set correctly. ```bash -go run main.go -provider=local --listener :8080 --temp-path=/tmp/ --basedir=/tmp/ +go run main.go --provider=local --listener :8080 --temp-path=/tmp/ --basedir=/tmp/ ``` ## Build diff --git a/server/storage.go b/server/storage.go index b8d9bc9..732af46 100644 --- a/server/storage.go +++ b/server/storage.go @@ -334,7 +334,7 @@ func NewGDriveStorage(clientJsonFilepath string, localConfigPath string, basedir return nil, err } - srv, err := drive.New(getGDriveClient(config)) + srv, err := drive.New(getGDriveClient(config, localConfigPath)) if err != nil { return nil, err } @@ -349,6 +349,7 @@ func NewGDriveStorage(clientJsonFilepath string, localConfigPath string, basedir } const GDriveRootConfigFile = "root_id.conf" +const GDriveTokenJsonFile = "token.json" const GDriveDirectoryMimeType = "application/vnd.google-apps.folder" func (s *GDrive) setupRoot() error { @@ -569,13 +570,14 @@ func (s *GDrive) Put(token string, filename string, reader io.Reader, contentTyp } // Retrieve a token, saves the token, then returns the generated client. -func getGDriveClient(config *oauth2.Config) *http.Client { - tokenFile := "token.json" +func getGDriveClient(config *oauth2.Config, localConfigPath string) *http.Client { + tokenFile := filepath.Join(localConfigPath, GDriveTokenJsonFile) tok, err := gDriveTokenFromFile(tokenFile) if err != nil { tok = getGDriveTokenFromWeb(config) saveGDriveToken(tokenFile, tok) } + return config.Client(context.Background(), tok) } @@ -590,7 +592,7 @@ func getGDriveTokenFromWeb(config *oauth2.Config) *oauth2.Token { log.Fatalf("Unable to read authorization code %v", err) } - tok, err := config.Exchange(oauth2.NoContext, authCode) + tok, err := config.Exchange(context.TODO(), authCode) if err != nil { log.Fatalf("Unable to retrieve token from web %v", err) }