Golang和Google Spreadsheet API V4-无法读取工作表

时间:2019-01-20 19:44:54

标签: go google-sheets-api google-oauth2

我一直在关注https://developers.google.com/sheets/api/quickstart/go,但无法使其正常工作。实际上,我一直都在收到此错误:

2019/01/20 14:39:00 Unable to retrieve data from sheet: Get https://sheets.googleapis.com/v4/spreadsheets/[..............]?alt=json&prettyPrint=false: oauth2: cannot fetch token: 401 Unauthorized
Response: {
    "error": "unauthorized_client",
    "error_description": "Unauthorized"
}

我首先更改了快速入门的 spreadsheetId readRange

// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
spreadsheetId := "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"
readRange := "Class Data!A2:E"
resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, readRange).Do()
if err != nil {
    log.Fatalf("Unable to retrieve data from sheet: %v", err)
}

if len(resp.Values) == 0 {
    fmt.Println("No data found.")
} else {
    fmt.Println("Name, Major:")
    for _, row := range resp.Values {
    // Print columns A and E, which correspond to indices 0 and 4.
        fmt.Printf("%s, %s\n", row[0], row[4])
    }
}

因为,由于我已经实现了Google Drive API V3,所以我放入了一个电子表格的ID(并使用了创建该电子表格的帐户的凭据。json)

但是,我仍然收到此错误,知道为什么吗?

感谢您的帮助

0 个答案:

没有答案