aboutsummaryrefslogtreecommitdiffstats
path: root/auth
diff options
context:
space:
mode:
Diffstat (limited to 'auth')
-rw-r--r--auth/file_source.go15
-rw-r--r--auth/oauth.go18
2 files changed, 31 insertions, 2 deletions
diff --git a/auth/file_source.go b/auth/file_source.go
index 5200203..11e7325 100644
--- a/auth/file_source.go
+++ b/auth/file_source.go
@@ -31,7 +31,7 @@ func (self *fileSource) Token() (*oauth2.Token, error) {
return token, nil
}
-func ReadToken(path string) (*oauth2.Token, bool, error) {
+func ReadFile(path string) ([]byte, bool, error) {
if !fileExists(path) {
return nil, false, nil
}
@@ -40,8 +40,19 @@ func ReadToken(path string) (*oauth2.Token, bool, error) {
if err != nil {
return nil, true, err
}
+ return content, true, nil
+}
+
+
+func ReadToken(path string) (*oauth2.Token, bool, error) {
+
+ content, exists, err := ReadFile(path)
+ if err != nil {
+ return nil, exists, err
+ }
+
token := &oauth2.Token{}
- return token, true, json.Unmarshal(content, token)
+ return token, exists, json.Unmarshal(content, token)
}
func SaveToken(path string, token *oauth2.Token) error {
diff --git a/auth/oauth.go b/auth/oauth.go
index 150642c..bc56738 100644
--- a/auth/oauth.go
+++ b/auth/oauth.go
@@ -3,6 +3,7 @@ package auth
import (
"fmt"
"golang.org/x/oauth2"
+ "golang.org/x/oauth2/google"
"net/http"
"time"
)
@@ -64,6 +65,23 @@ func NewAccessTokenClient(clientId, clientSecret, accessToken string) *http.Clie
)
}
+func NewServiceAccountClient(serviceAccountFile string) (*http.Client, error) {
+ content, exists, err := ReadFile(serviceAccountFile)
+ if(!exists) {
+ return nil, fmt.Errorf("Service account filename %q not found", serviceAccountFile)
+ }
+
+ if(err != nil) {
+ return nil, err
+ }
+
+ conf, err := google.JWTConfigFromJSON(content, "https://www.googleapis.com/auth/drive")
+ if(err != nil) {
+ return nil, err
+ }
+ return conf.Client(oauth2.NoContext), nil
+}
+
func getConfig(clientId, clientSecret string) *oauth2.Config {
return &oauth2.Config{
ClientID: clientId,