aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetter Rasmussen2014-05-14 19:34:16 +0200
committerPetter Rasmussen2014-05-14 19:34:16 +0200
commit63169fef51dab3ca4f55775640bfe5b202fcc042 (patch)
tree52a0b9ebec26a3dbf6fd3b15db3971cba898c162
parent79cbfc2986f4c8152de187ae2be207829d08077f (diff)
parentf14443b6e7b1d0c67332c4ceb3db0970d0d89705 (diff)
downloadgdrive-63169fef51dab3ca4f55775640bfe5b202fcc042.tar.bz2
Merge pull request #17 from singhsaysdotcom/master
Make interactive oauth authentication optional, for automation use cases
-rw-r--r--auth/auth.go7
-rw-r--r--drive.go2
-rw-r--r--gdrive/gdrive.go4
3 files changed, 9 insertions, 4 deletions
diff --git a/auth/auth.go b/auth/auth.go
index ed65920..6bb14d6 100644
--- a/auth/auth.go
+++ b/auth/auth.go
@@ -2,6 +2,7 @@ package auth
import (
"code.google.com/p/goauth2/oauth"
+ "errors"
"fmt"
"github.com/prasmussen/gdrive/util"
"net/http"
@@ -35,7 +36,7 @@ func hasValidToken(cacheFile oauth.CacheFile, transport *oauth.Transport) bool {
return true
}
-func GetOauth2Client(clientId, clientSecret, cachePath string) (*http.Client, error) {
+func GetOauth2Client(clientId, clientSecret, cachePath string, promptUser bool) (*http.Client, error) {
cacheFile := oauth.CacheFile(cachePath)
config := &oauth.Config{
@@ -58,6 +59,10 @@ func GetOauth2Client(clientId, clientSecret, cachePath string) (*http.Client, er
return transport.Client(), nil
}
+ if !promptUser {
+ return nil, errors.New("no valid token found")
+ }
+
// Get auth code from user and request a new token
code := promptUserForAuthCode(config)
_, err := transport.Exchange(code)
diff --git a/drive.go b/drive.go
index 05d1da0..f32ad9c 100644
--- a/drive.go
+++ b/drive.go
@@ -85,7 +85,7 @@ func main() {
}
// Get authorized drive client
- drive, err := gdrive.New(opts.AppPath, opts.Advanced)
+ drive, err := gdrive.New(opts.AppPath, opts.Advanced, true)
if err != nil {
writeError("An error occurred creating Drive client: %v\n", err)
}
diff --git a/gdrive/gdrive.go b/gdrive/gdrive.go
index 883ebaf..45458b2 100644
--- a/gdrive/gdrive.go
+++ b/gdrive/gdrive.go
@@ -28,7 +28,7 @@ func (self *Drive) Client() *http.Client {
return self.client
}
-func New(customAppPath string, advancedMode bool) (*Drive, error) {
+func New(customAppPath string, advancedMode bool, promptUser bool) (*Drive, error) {
if customAppPath != "" {
AppPath = customAppPath
}
@@ -38,7 +38,7 @@ func New(customAppPath string, advancedMode bool) (*Drive, error) {
tokenPath := filepath.Join(AppPath, TokenFname)
config := config.Load(configPath, advancedMode)
- client, err := auth.GetOauth2Client(config.ClientId, config.ClientSecret, tokenPath)
+ client, err := auth.GetOauth2Client(config.ClientId, config.ClientSecret, tokenPath, promptUser)
if err != nil {
return nil, err
}