谷歌文档 – 访问数据

huangapple go评论90阅读模式
英文:

Google docs - access data

问题

我想访问公共谷歌文档中的数据。

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

链接有效。

我正在使用的代码:

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("无法获取文档客户端:%v", err)
	}

	// 打印请求文档的标题:
	// 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("无法从文档中检索数据:%v", err)
	}
	fmt.Printf("文档的标题是:%s\n", doc.Title)
}

它返回404未找到。我做错了什么?

英文:

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?

答案1

得分: 1

当我看到你展示的URL https://docs.google.com/spreadsheets/d/12i3Lvwb_14fQES27jVP6baqqmWUXWwM7fZy-neDH3bE/edit#gid=0 时,它是用于Google电子表格,而不是Google文档。所以在这种情况下,无法使用Google Docs API检索此文件。请注意这一点。

当您想从这个公开共享的Google电子表格中检索值时,可以考虑以下修改。在这种情况下,使用Sheets API v4。请注意这一点。

修改后的脚本:

在当前阶段,没有API密钥和访问令牌,无法使用Sheets API v4。这似乎是当前的规范。所以我认为在您的脚本中,即使电子表格是公开共享的,您的srv也无法用于从Google电子表格中检索值。我认为您目前的问题可能是由于这种情况。在您的情况下,您的电子表格是公开共享的。因此,为了检索值,可以使用以下API。

package main

import (
	"context"
	"fmt"
	"log"

	"google.golang.org/api/option"
	"google.golang.org/api/sheets/v4"
)

func main() {
	APIkey := "###" // 请设置您的API密钥。

	ctx := context.Background()
	srv, err := sheets.NewService(ctx, option.WithAPIKey(APIkey))
	if err != nil {
		log.Fatalf("无法检索文档客户端:%v", err)
	}

	spreadsheetId := "12i3Lvwb_14fQES27jVP6baqqmWUXWwM7fZy-neDH3bE"
	res, err := srv.Spreadsheets.Get(spreadsheetId).Do()
	if err != nil {
		log.Fatalf("无法从文档中检索数据:%v", err)
	}
	fmt.Printf("文档的标题是:%s\n", res.Properties.Title)
}

结果:

当使用有效的API密钥运行此脚本时,将获得以下结果。

文档的标题是:test

注意:

  • 如果您的示例是公开共享的Google文档,不幸的是,在当前阶段,即使文档是公开共享的,也无法使用API密钥使用Google Docs API。这似乎是当前的规范。请注意这一点。在这种情况下,请使用访问令牌。可以在这里查看此功能的示例脚本。

参考:

英文:

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:

huangapple
  • 本文由 发表于 2022年2月3日 04:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/70962304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定