通过Golang SDK分享Google文档

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

Share Google Doc via Golang SDK

问题

我正在使用服务账号 JSON 通过 Golang SDK 创建 Google 文档,但由于该文档只能由服务账号访问,我无法使用我的个人 Google 账号访问它。在 Google 文档 SDK 文档中,我找不到任何分享文档的函数。

这是我的示例代码:

package googledoc

import (
	"context"
	"fmt"

	log "github.com/sirupsen/logrus"
	"golang.org/x/oauth2/google"
	"google.golang.org/api/docs/v1"
	"google.golang.org/api/option"
)

func CreateDoc(title string) error {
	ctx := context.Background()
	cred, err := GetSecrets("ap-south-1", "GOOGLE_SVC_ACC_JSON")
	if err != nil {
		return fmt.Errorf("无法获取 SSM %v", err)
	}

	config, err := google.JWTConfigFromJSON(cred, docs.DocumentsScope)
	if err != nil {
		log.Fatalf("无法将客户端密钥文件解析为配置:%v", err)
	}
	client := config.Client(ctx)
	srv, err := docs.NewService(ctx, option.WithHTTPClient(client))
	if err != nil {
		log.Fatalf("无法获取 Docs 客户端:%v", err)
	}

	docObj := docs.Document{
		Title: title,
	}

	doc, err := srv.Documents.Create(&docObj).Do()
	if err != nil {
		log.Fatalf("无法从文档中检索数据:%v", err)
		return err
	}
	fmt.Printf("文档的标题是:%s %s\n", doc.Title, doc.DocumentId)
	return nil
}

希望对你有所帮助,谢谢。

英文:

I am using service account JSON to create google Doc via Golang SDK but as this doc is only accessible to Service account I am not able to access it with my Personal Google Account. In the Google Doc SDK documentation I couldn't find any function to share the Doc.

This is my sample Code:

package googledoc

import (
	"context"
	"fmt"

	log "github.com/sirupsen/logrus"
	"golang.org/x/oauth2/google"
	"google.golang.org/api/docs/v1"
	"google.golang.org/api/option"

func CreateDoc(title string) error {
	ctx := context.Background()
	cred, err := GetSecrets("ap-south-1", "GOOGLE_SVC_ACC_JSON")
	if err != nil {
		return fmt.Errorf("unable to get the SSM %v", err)
	}

	config, err := google.JWTConfigFromJSON(cred, docs.DocumentsScope)
	if err != nil {
		log.Fatalf("Unable to parse client secret file to config: %v", err)
	}
	client := config.Client(ctx)
	srv, err := docs.NewService(ctx, option.WithHTTPClient(client))
	if err != nil {
		log.Fatalf("Unable to retrieve Docs client: %v", err)
	}

	docObj := docs.Document{
		Title: title,
	}

	doc, err := srv.Documents.Create(&docObj).Do()
	if err != nil {
		log.Fatalf("Unable to retrieve data from document: %v", err)
		return err
	}
	fmt.Printf("The title of the doc is: %s %s\n", doc.Title, doc.DocumentId)
	return nil
}

Any help with this would be really appreciated Thanks.

答案1

得分: 3

我相信你的目标如下:

  • 你想通过服务账号与你的Google账号共享创建的Google文档。
  • 你想使用googleapis来实现这一目标。

不幸的是,Google Docs API不能用于与用户共享文档。在这种情况下,可以使用Drive API。当你的脚本使用Drive API进行修改时,可以尝试以下修改:

示例脚本:

请将此脚本添加到fmt.Printf("The title of the doc is: %s %s\n", doc.Title, doc.DocumentId)这一行之后。通过这样做,创建的Google文档将与用户共享。

driveSrv, err := drive.NewService(ctx, option.WithHTTPClient(client)) // 请使用你的客户端和服务来使用Drive API。
if err != nil {
	log.Fatal(err)
}
permission := &drive.Permission{
	EmailAddress: "###", // 请设置你想要共享的电子邮件地址。
	Role:         "writer",
	Type:         "user",
}
res, err := driveSrv.Permissions.Create(doc.DocumentId, permission).Do()
if err != nil {
	log.Fatal(err)
	return err
}
fmt.Println(res)
  • 当将此脚本添加到你的脚本中时,创建的文档将以写入者的身份与用户共享。

参考:

英文:

I believe your goal is as follows.

  • You want to share the created Google Document by the service account with your Google account.
  • You want to achieve this using googleapis for golang.

Unfortunately, Google Docs API cannot be used for sharing the Document with users. In this case, Drive API is used. When your script is modified using Drive API, how about the following modification?

Sample script:

Please add this script just after the line of fmt.Printf("The title of the doc is: %s %s\n", doc.Title, doc.DocumentId). By this, the created Google Document is shared with the user.

driveSrv, err := drive.NewService(ctx, option.WithHTTPClient(client)) // Please use your client and service for using Drive API.
if err != nil {
	log.Fatal(err)
}
permission := &drive.Permission{
	EmailAddress: "###", // Please set the email address you want to share.
	Role:         "writer",
	Type:         "user",
}
res, err := driveSrv.Permissions.Create(doc.DocumentId, permission).Do()
if err != nil {
	log.Fatal(err)
	return err
}
fmt.Println(res)
  • When this script is added to your script, the created Document is shared with the user as the writer.

Reference:

huangapple
  • 本文由 发表于 2022年9月7日 18:01:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/73633549.html
匿名

发表评论

匿名网友

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

确定