Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit fe54172

Browse files
committed
Sheets API: Simplify quickstart code by using option.WithCredentialsFile().
1 parent 35bd416 commit fe54172

File tree

1 file changed

+7
-75
lines changed

1 file changed

+7
-75
lines changed

sheets/quickstart/quickstart.go

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,88 +18,20 @@
1818
package main
1919

2020
import (
21-
"encoding/json"
21+
"context"
2222
"fmt"
23-
"io/ioutil"
2423
"log"
25-
"net/http"
26-
"os"
2724

28-
"golang.org/x/net/context"
29-
"golang.org/x/oauth2"
30-
"golang.org/x/oauth2/google"
25+
"google.golang.org/api/option"
3126
"google.golang.org/api/sheets/v4"
3227
)
3328

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-
8929
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()
10131

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"))
10335
if err != nil {
10436
log.Fatalf("Unable to retrieve Sheets client: %v", err)
10537
}
@@ -108,7 +40,7 @@ func main() {
10840
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
10941
spreadsheetId := "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"
11042
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()
11244
if err != nil {
11345
log.Fatalf("Unable to retrieve data from sheet: %v", err)
11446
}

0 commit comments

Comments
 (0)