This repository has been archived by the owner on Jun 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
105 lines (94 loc) · 2.92 KB
/
token.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
99
100
101
102
103
104
105
/**
* Copyright (c) Google Developers Site
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This file copied from https://developers.google.com/drive/api/v3/quickstart/go and modified.
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/kelseyhightower/envconfig"
"golang.org/x/oauth2"
"google.golang.org/api/drive/v3"
)
func getOAuth2Config() (*oauth2.Config, error) {
config := struct {
GoogleClientID string `envconfig:"GOOGLE_CLIENT_ID",required:"true"`
GoogleClientSecret string `envconfig:"GOOGLE_CLIENT_SECRET",required:"true"`
}{}
if err := envconfig.Process("DT", &config); err != nil {
return nil, err
}
return &oauth2.Config{
ClientID: config.GoogleClientID,
ClientSecret: config.GoogleClientSecret,
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
Scopes: []string{drive.DriveScope},
Endpoint: oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://oauth2.googleapis.com/token",
},
}, nil
}
func getTokenFromWeb(config *oauth2.Config) (*oauth2.Token, error) {
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
if _, err := fmt.Scan(&authCode); err != nil {
return nil, fmt.Errorf("Unable to read authorization code %v", err)
}
tok, err := config.Exchange(context.TODO(), authCode)
if err != nil {
return nil, fmt.Errorf("Unable to retrieve token from web %v", err)
}
return tok, nil
}
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}
func saveToken(path string, token *oauth2.Token) error {
fmt.Printf("Saving credential file to: %s\n", path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("Unable to cache oauth token: %v", err)
}
defer f.Close()
json.NewEncoder(f).Encode(token)
return nil
}
func getClient(config *oauth2.Config) (*http.Client, error) {
tokFile := "token.json"
tok, err := tokenFromFile(tokFile)
if err != nil {
tok, err := getTokenFromWeb(config)
if err != nil {
return nil, err
}
if err := saveToken(tokFile, tok); err != nil {
return nil, err
}
}
return config.Client(context.Background(), tok), nil
}