mirror of
https://github.com/gilbertchen/duplicacy
synced 2025-12-06 00:03:38 +00:00
Access Google Drive via service account.
Our GCS backend already supports service account. This just copies relevant code from there.
This commit is contained in:
@@ -20,8 +20,10 @@ import (
|
|||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
|
"golang.org/x/oauth2/google"
|
||||||
"google.golang.org/api/drive/v3"
|
"google.golang.org/api/drive/v3"
|
||||||
"google.golang.org/api/googleapi"
|
"google.golang.org/api/googleapi"
|
||||||
|
"google.golang.org/api/option"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -315,25 +317,50 @@ func (storage *GCDStorage) getIDFromPath(threadIndex int, filePath string, creat
|
|||||||
// CreateGCDStorage creates a GCD storage object.
|
// CreateGCDStorage creates a GCD storage object.
|
||||||
func CreateGCDStorage(tokenFile string, driveID string, storagePath string, threads int) (storage *GCDStorage, err error) {
|
func CreateGCDStorage(tokenFile string, driveID string, storagePath string, threads int) (storage *GCDStorage, err error) {
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
description, err := ioutil.ReadFile(tokenFile)
|
description, err := ioutil.ReadFile(tokenFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
gcdConfig := &GCDConfig{}
|
var object map[string]interface{}
|
||||||
if err := json.Unmarshal(description, gcdConfig); err != nil {
|
|
||||||
|
err = json.Unmarshal(description, &object)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
oauth2Config := oauth2.Config{
|
isServiceAccount := false
|
||||||
ClientID: gcdConfig.ClientID,
|
if value, ok := object["type"]; ok {
|
||||||
ClientSecret: gcdConfig.ClientSecret,
|
if authType, ok := value.(string); ok && authType == "service_account" {
|
||||||
Endpoint: gcdConfig.Endpoint,
|
isServiceAccount = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
authClient := oauth2Config.Client(context.Background(), &gcdConfig.Token)
|
var tokenSource oauth2.TokenSource
|
||||||
|
|
||||||
service, err := drive.New(authClient)
|
if isServiceAccount {
|
||||||
|
config, err := google.JWTConfigFromJSON(description, drive.DriveScope)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tokenSource = config.TokenSource(ctx)
|
||||||
|
} else {
|
||||||
|
gcdConfig := &GCDConfig{}
|
||||||
|
if err := json.Unmarshal(description, gcdConfig); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
config := oauth2.Config{
|
||||||
|
ClientID: gcdConfig.ClientID,
|
||||||
|
ClientSecret: gcdConfig.ClientSecret,
|
||||||
|
Endpoint: gcdConfig.Endpoint,
|
||||||
|
}
|
||||||
|
tokenSource = config.TokenSource(ctx, &gcdConfig.Token)
|
||||||
|
}
|
||||||
|
|
||||||
|
service, err := drive.NewService(ctx, option.WithTokenSource(tokenSource))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user