18
18
package main
19
19
20
20
import (
21
- "encoding/json "
21
+ "context "
22
22
"fmt"
23
- "io/ioutil"
24
23
"log"
25
- "net/http"
26
- "os"
27
24
28
- "golang.org/x/net/context"
29
- "golang.org/x/oauth2"
30
- "golang.org/x/oauth2/google"
25
+ "google.golang.org/api/option"
31
26
"google.golang.org/api/sheets/v4"
32
27
)
33
28
34
- // Retrieve a token, saves the token, then returns the generated client.
35
- func getClient (config * oauth2.Config ) * http.Client {
36
- // The file token.json stores the user's access and refresh tokens, and is
37
- // created automatically when the authorization flow completes for the first
38
- // time.
39
- tokFile := "token.json"
40
- tok , err := tokenFromFile (tokFile )
41
- if err != nil {
42
- tok = getTokenFromWeb (config )
43
- saveToken (tokFile , tok )
44
- }
45
- return config .Client (context .Background (), tok )
46
- }
47
-
48
- // Request a token from the web, then returns the retrieved token.
49
- func getTokenFromWeb (config * oauth2.Config ) * oauth2.Token {
50
- authURL := config .AuthCodeURL ("state-token" , oauth2 .AccessTypeOffline )
51
- fmt .Printf ("Go to the following link in your browser then type the " +
52
- "authorization code: \n %v\n " , authURL )
53
-
54
- var authCode string
55
- if _ , err := fmt .Scan (& authCode ); err != nil {
56
- log .Fatalf ("Unable to read authorization code: %v" , err )
57
- }
58
-
59
- tok , err := config .Exchange (context .TODO (), authCode )
60
- if err != nil {
61
- log .Fatalf ("Unable to retrieve token from web: %v" , err )
62
- }
63
- return tok
64
- }
65
-
66
- // Retrieves a token from a local file.
67
- func tokenFromFile (file string ) (* oauth2.Token , error ) {
68
- f , err := os .Open (file )
69
- if err != nil {
70
- return nil , err
71
- }
72
- defer f .Close ()
73
- tok := & oauth2.Token {}
74
- err = json .NewDecoder (f ).Decode (tok )
75
- return tok , err
76
- }
77
-
78
- // Saves a token to a file path.
79
- func saveToken (path string , token * oauth2.Token ) {
80
- fmt .Printf ("Saving credential file to: %s\n " , path )
81
- f , err := os .OpenFile (path , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0600 )
82
- if err != nil {
83
- log .Fatalf ("Unable to cache oauth token: %v" , err )
84
- }
85
- defer f .Close ()
86
- json .NewEncoder (f ).Encode (token )
87
- }
88
-
89
29
func main () {
90
- b , err := ioutil .ReadFile ("credentials.json" )
91
- if err != nil {
92
- log .Fatalf ("Unable to read client secret file: %v" , err )
93
- }
94
-
95
- // If modifying these scopes, delete your previously saved token.json.
96
- config , err := google .ConfigFromJSON (b , "https://www.googleapis.com/auth/spreadsheets.readonly" )
97
- if err != nil {
98
- log .Fatalf ("Unable to parse client secret file to config: %v" , err )
99
- }
100
- client := getClient (config )
30
+ ctx := context .Background ()
101
31
102
- srv , err := sheets .New (client )
32
+ // Authenticate with a service account or refresh token JSON credentials file.
33
+ // Remove the option to authenticate via Application Default Credentials.
34
+ srv , err := sheets .NewService (ctx , option .WithCredentialsFile ("credentials.json" ))
103
35
if err != nil {
104
36
log .Fatalf ("Unable to retrieve Sheets client: %v" , err )
105
37
}
@@ -108,7 +40,7 @@ func main() {
108
40
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
109
41
spreadsheetId := "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"
110
42
readRange := "Class Data!A2:E"
111
- resp , err := srv .Spreadsheets .Values .Get (spreadsheetId , readRange ).Do ()
43
+ resp , err := srv .Spreadsheets .Values .Get (spreadsheetId , readRange ).Context ( ctx ). Do ()
112
44
if err != nil {
113
45
log .Fatalf ("Unable to retrieve data from sheet: %v" , err )
114
46
}
0 commit comments