-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdrive.go
98 lines (84 loc) · 2.24 KB
/
gdrive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"encoding/json"
"fmt"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v3"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
const DriveClientTimeout = 30 * time.Minute
var driveClient *drive.Service
var driveClientEndtime time.Time
func getDrive() *drive.Service {
if driveClientEndtime.After(time.Now()) {
driveClient = nil
}
if driveClient != nil {
return driveClient
}
buffer, err := ioutil.ReadFile("etc/client_secret.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
config, err := google.ConfigFromJSON(buffer, drive.DriveScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
driveClient, err = drive.New(getClient(config))
if err != nil {
log.Fatalf("Unable to retrieve drive client: %v", err)
}
driveClientEndtime = time.Now().Add(time.Duration(DriveClientTimeout))
return driveClient
}
func getClient(config *oauth2.Config) *http.Client {
tokenFile := "etc/token.json"
tok, err := tokenFromFile(tokenFile)
if err != nil {
tok = getTokenFromWeb(config)
saveToken(tokenFile, tok)
}
return config.Client(context.Background(), tok)
}
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
authUrl := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the "+
"authorization code: \n%v\n", authUrl)
var authCode string
_, err := fmt.Scan(&authCode)
if err != nil {
log.Fatalf("Unable to read authorization code: %v", err)
}
tok, err := config.Exchange(context.Background(), authCode)
if err != nil {
log.Fatalf("Unable to retrieve token from web: %v", err)
}
return tok
}
func saveToken(file string, tok *oauth2.Token) {
f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
defer f.Close()
if err != nil {
log.Fatalf("Unable to save oauth token: %v", err)
}
err = json.NewEncoder(f).Encode(tok)
if err != nil {
log.Fatalf("Unable to save oauth token: %v", err)
}
}
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
defer f.Close()
if err != nil {
return nil, err
}
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}