Search code examples
gogoogle-docs

Google docs - access data


I want to access the data from public google docs.

https://docs.google.com/spreadsheets/d/12i3Lvwb_14fQES27jVP6baqqmWUXWwM7fZy-neDH3bE/edit#gid=0

The link is working.

The code I'm using:

package main

import (
    "context"
    "fmt"
    "log"

    "google.golang.org/api/docs/v1"
    "google.golang.org/api/option"
)

var docURL = "https://docs.google.com/spreadsheets/d/12i3Lvwb_14fQES27jVP6baqqmWUXWwM7fZy-neDH3bE/edit#gid=0"

func main() {
    ctx := context.Background()

    srv, err := docs.NewService(ctx, option.WithoutAuthentication(), option.WithEndpoint(docURL))
    if err != nil {
        log.Fatalf("Unable to retrieve Docs client: %v", err)
    }

    // Prints the title of the requested doc:
    // https://docs.google.com/spreadsheets/d/12i3Lvwb_14fQES27jVP6baqqmWUXWwM7fZy-neDH3bE/edit#gid=0
    docId := "12i3Lvwb_14fQES27jVP6baqqmWUXWwM7fZy-neDH3bE"
    doc, err := srv.Documents.Get(docId).Do()
    if err != nil {
        log.Fatalf("Unable to retrieve data from document: %v", err)
    }
    fmt.Printf("The title of the doc is: %s\n", doc.Title)
}

It returns 404 not found. What I'm doing wrong?


Solution

  • When I saw your showing URL of https://docs.google.com/spreadsheets/d/12i3Lvwb_14fQES27jVP6baqqmWUXWwM7fZy-neDH3bE/edit#gid=0, it's for Google Spreadsheet which is not Google Document. So in this case, this file cannot be retrieved using Google Docs API. Please be careful about this.

    When you want to retrieve the values from this publicly shared Google Spreadsheet, how about the following modification? In this case, Sheets API v4 is used. Please be careful about this.

    Modified script:

    In the current stage, Sheets API v4 cannot be used without the API key and the access token. It seems that this is the current specification. So I thought that in your script, your srv cannot be used for retrieving the values from the Google Spreadsheet even when that is publicly shared. I thought that your current issue might be due to this situation. In your situation, your Spreadsheet is publicly shared. So in order to retrieve the values, the API can be used as follows.

    package main
    
    import (
        "context"
        "fmt"
        "log"
    
        "google.golang.org/api/option"
        "google.golang.org/api/sheets/v4"
    )
    
    func main() {
        APIkey := "###" // Please set your API key.
    
        ctx := context.Background()
        srv, err := sheets.NewService(ctx, option.WithAPIKey(APIkey))
        if err != nil {
            log.Fatalf("Unable to retrieve Docs client: %v", err)
        }
    
        spreadsheetId := "12i3Lvwb_14fQES27jVP6baqqmWUXWwM7fZy-neDH3bE"
        res, err := srv.Spreadsheets.Get(spreadsheetId).Do()
        if err != nil {
            log.Fatalf("Unable to retrieve data from document: %v", err)
        }
        fmt.Printf("The title of the doc is: %s\n", res.Properties.Title)
    }
    

    Result:

    When this script is run using the valid API key, the following result is obtained.

    The title of the doc is: test
    

    Note:

    • If your sample is the publicly sharead Google Document, unfortunately, in the current stage, Google Docs API cannot be used with the API key even when the Document is publicly shared. It seems that this is the current specification. Please be careful this. In that case, please use the access token. The sample script for this can be seen at here.

    Reference: