diff options
| author | Fabio Di Fabio | 2017-01-26 15:03:51 +0100 |
|---|---|---|
| committer | Fabio Di Fabio | 2017-01-26 15:03:51 +0100 |
| commit | b33b3e96eb6443ae3dff61e0e7b38bb529e328ea (patch) | |
| tree | aec26d4e9e10bb0abcb405c84facf8217af68fc7 /auth/oauth.go | |
| parent | 0e05cfb6f84a714f9bdafade6d3cddbb8c8273e9 (diff) | |
| download | gdrive-b33b3e96eb6443ae3dff61e0e7b38bb529e328ea.tar.bz2 | |
New feature, support authentication via service account.
Diffstat (limited to 'auth/oauth.go')
| -rw-r--r-- | auth/oauth.go | 18 |
1 files changed, 18 insertions, 0 deletions
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, |
