Created
September 1, 2019 22:39
-
-
Save bkono/ab7643bce7b4cd492fdfccaf706c603d to your computer and use it in GitHub Desktop.
Accessing a Google Sheet with service account credentials from the download JSON.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"io/ioutil" | |
"log" | |
"golang.org/x/oauth2/google" | |
"google.golang.org/api/sheets/v4" | |
) | |
func main() { | |
b, err := ioutil.ReadFile("creds.json") | |
if err != nil { | |
log.Fatalf("Unable to read client secret file: %v", err) | |
} | |
cfg, err := google.JWTConfigFromJSON(b, sheets.SpreadsheetsScope) | |
if err != nil { | |
log.Fatalf("while getting config: %v", err) | |
} | |
client := cfg.Client(context.Background()) | |
cl, err := sheets.New(client) | |
if err != nil { | |
log.Fatalf("while creating sheets service: %v", err) | |
} | |
spreadsheetID := "SOMESPREADSHEETID" | |
readRange := "Sheet1!A1:B2" | |
rsp, err := cl.Spreadsheets.Values.Get(spreadsheetID, readRange).Do() | |
if err != nil { | |
log.Fatalf("while getting sheet values: %v", err) | |
} | |
if len(rsp.Values) == 0 { | |
log.Fatalln("0 rows returned") | |
} | |
for _, row := range rsp.Values { | |
log.Printf("%s, %s\n", row[0], row[1]) | |
} | |
log.Printf("%+v", rsp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment