finding old code from 2020-10-19
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
c11fd878cd
commit
a971fe90c7
1 changed files with 78 additions and 79 deletions
157
main.go
157
main.go
|
@ -1,109 +1,108 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/oauth2/google"
|
"golang.org/x/oauth2/google"
|
||||||
"google.golang.org/api/sheets/v4"
|
"google.golang.org/api/sheets/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Retrieve a token, saves the token, then returns the generated client.
|
// Retrieve a token, saves the token, then returns the generated client.
|
||||||
func getClient(config *oauth2.Config) *http.Client {
|
func getClient(config *oauth2.Config) *http.Client {
|
||||||
// The file token.json stores the user's access and refresh tokens, and is
|
// The file token.json stores the user's access and refresh tokens, and is
|
||||||
// created automatically when the authorization flow completes for the first
|
// created automatically when the authorization flow completes for the first
|
||||||
// time.
|
// time.
|
||||||
tokFile := "token.json"
|
tokFile := "token.json"
|
||||||
tok, err := tokenFromFile(tokFile)
|
tok, err := tokenFromFile(tokFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tok = getTokenFromWeb(config)
|
tok = getTokenFromWeb(config)
|
||||||
saveToken(tokFile, tok)
|
saveToken(tokFile, tok)
|
||||||
}
|
}
|
||||||
return config.Client(context.Background(), tok)
|
return config.Client(context.Background(), tok)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request a token from the web, then returns the retrieved token.
|
// Request a token from the web, then returns the retrieved token.
|
||||||
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
|
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
|
||||||
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
|
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
|
||||||
fmt.Printf("Go to the following link in your browser then type the "+
|
fmt.Printf("Go to the following link in your browser then type the "+
|
||||||
"authorization code: \n%v\n", authURL)
|
"authorization code: \n%v\n", authURL)
|
||||||
|
|
||||||
var authCode string
|
var authCode string
|
||||||
if _, err := fmt.Scan(&authCode); err != nil {
|
if _, err := fmt.Scan(&authCode); err != nil {
|
||||||
log.Fatalf("Unable to read authorization code: %v", err)
|
log.Fatalf("Unable to read authorization code: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tok, err := config.Exchange(context.TODO(), authCode)
|
tok, err := config.Exchange(context.TODO(), authCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to retrieve token from web: %v", err)
|
log.Fatalf("Unable to retrieve token from web: %v", err)
|
||||||
}
|
}
|
||||||
return tok
|
return tok
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieves a token from a local file.
|
// Retrieves a token from a local file.
|
||||||
func tokenFromFile(file string) (*oauth2.Token, error) {
|
func tokenFromFile(file string) (*oauth2.Token, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
tok := &oauth2.Token{}
|
tok := &oauth2.Token{}
|
||||||
err = json.NewDecoder(f).Decode(tok)
|
err = json.NewDecoder(f).Decode(tok)
|
||||||
return tok, err
|
return tok, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Saves a token to a file path.
|
// Saves a token to a file path.
|
||||||
func saveToken(path string, token *oauth2.Token) {
|
func saveToken(path string, token *oauth2.Token) {
|
||||||
fmt.Printf("Saving credential file to: %s\n", path)
|
fmt.Printf("Saving credential file to: %s\n", path)
|
||||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to cache oauth token: %v", err)
|
log.Fatalf("Unable to cache oauth token: %v", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
json.NewEncoder(f).Encode(token)
|
json.NewEncoder(f).Encode(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
b, err := ioutil.ReadFile("credentials.json")
|
b, err := ioutil.ReadFile("credentials.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to read client secret file: %v", err)
|
log.Fatalf("Unable to read client secret file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If modifying these scopes, delete your previously saved token.json.
|
// If modifying these scopes, delete your previously saved token.json.
|
||||||
config, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/spreadsheets.readonly")
|
config, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/spreadsheets.readonly")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to parse client secret file to config: %v", err)
|
log.Fatalf("Unable to parse client secret file to config: %v", err)
|
||||||
}
|
}
|
||||||
client := getClient(config)
|
client := getClient(config)
|
||||||
|
|
||||||
srv, err := sheets.New(client)
|
srv, err := sheets.New(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to retrieve Sheets client: %v", err)
|
log.Fatalf("Unable to retrieve Sheets client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the names and majors of students in a sample spreadsheet:
|
// Prints the names and majors of students in a sample spreadsheet:
|
||||||
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
|
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
|
||||||
spreadsheetId := "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"
|
spreadsheetId := "1gAn7JyASTCydfC2ZllUx4qpS2hMTd5TWVykq6AVAf-c"
|
||||||
readRange := "Class Data!A2:E"
|
readRange := "Sheet1!A2:E"
|
||||||
resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, readRange).Do()
|
resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, readRange).Do()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to retrieve data from sheet: %v", err)
|
log.Fatalf("Unable to retrieve data from sheet: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(resp.Values) == 0 {
|
if len(resp.Values) == 0 {
|
||||||
fmt.Println("No data found.")
|
fmt.Println("No data found.")
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Name, Major:")
|
fmt.Println("Name, Major:")
|
||||||
for _, row := range resp.Values {
|
for _, row := range resp.Values {
|
||||||
// Print columns A and E, which correspond to indices 0 and 4.
|
// Print columns A and E, which correspond to indices 0 and 4.
|
||||||
fmt.Printf("%s, %s\n", row[0], row[4])
|
fmt.Printf("%q\n", row)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue