Skip to content

Commit

Permalink
Add support for user-provided access token
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Feb 20, 2016
1 parent 21cc148 commit c1960cd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ func NewRefreshTokenClient(clientId, clientSecret, refreshToken string) *http.Cl
)
}

func NewAccessTokenClient(clientId, clientSecret, accessToken string) *http.Client {
conf := getConfig(clientId, clientSecret)

token := &oauth2.Token{
TokenType: "Bearer",
AccessToken: accessToken,
}

return oauth2.NewClient(
oauth2.NoContext,
conf.TokenSource(oauth2.NoContext, token),
)
}

func getConfig(clientId, clientSecret string) *oauth2.Config {
return &oauth2.Config{
ClientID: clientId,
Expand Down
5 changes: 5 additions & 0 deletions gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func main() {
Patterns: []string{"--refresh-token"},
Description: "Oauth refresh token used to get access token (for advanced users)",
},
cli.StringFlag{
Name: "accessToken",
Patterns: []string{"--access-token"},
Description: "Oauth access token, only recommended for short-lived requests because of short lifetime (for advanced users)",
},
}

handlers := []*cli.Handler{
Expand Down
8 changes: 8 additions & 0 deletions handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,18 @@ func aboutExportHandler(ctx cli.Context) {
}

func getOauthClient(args cli.Arguments) (*http.Client, error) {
if args.String("refreshToken") != "" && args.String("accessToken") != "" {
ExitF("Access token not needed when refresh token is provided")
}

if args.String("refreshToken") != "" {
return auth.NewRefreshTokenClient(ClientId, ClientSecret, args.String("refreshToken")), nil
}

if args.String("accessToken") != "" {
return auth.NewAccessTokenClient(ClientId, ClientSecret, args.String("accessToken")), nil
}

configDir := args.String("configDir")
tokenPath := ConfigFilePath(configDir, TokenFilename)
return auth.NewFileSourceClient(ClientId, ClientSecret, tokenPath, authCodePrompt)
Expand Down

0 comments on commit c1960cd

Please sign in to comment.