在python中,是否可以编辑公共共享的Google工作表,而无需使用身份验证代码? [英] In python, is it possible to edit a publicy shared google sheet, without an auth code?
问题描述
我设置了一个Google工作表,以便任何人都可以编辑.
I have a google sheet which I set so that anyone can edit.
我注意到,在 gspread
包中,您可以单独从链接中获取电子表格.示例:
I noticed that in the gspread
package, you can get a spreadsheet from the link alone. Example:
sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0')
由于任何人都可以编辑,因此无需提供用于访问的身份验证代码.但是,似乎总是需要使用auth代码对其进行初始化.
Since anyone can edit, providing an auth code for access is not necessary. However, it seems that you always need to initialize it using an auth code.
是否有一种无需任何验证码即可编辑Google表格的方法?
Is there a way to edit a google sheet without any auth code?
This may get confused as a duplicate of this post Using gspread to read from a Google Drive Spreadsheet without logging in however, the solutions are to either have a client provide their auth code, or download the file directly, but no option to edit the file.
推荐答案
- 您要编辑Google Spreadsheet,而无需登录Google来获取授权码.
- 您想通过gspread和python实现此目的.
- 您已经能够使用Sheets API获取和放置Google Spreadsheet的值.
- 创建服务帐户并下载JSON文件以使用该服务帐户.
- 将服务帐户的电子邮件地址共享到您要使用的Google Spreadsheet.
- 您可以在下载的JSON文件中查看电子邮件地址.
如果我的理解是正确的,该解决方法如何?请认为这只是几种解决方法之一.
If my understanding is correct, how about this workaround? Please think of this as just one of several workarounds.
在此替代方法中,将使用服务帐户.使用服务帐户后,无需使用gspread登录Google帐户即可获取和放置Google Spreadsheet的值.
In this workaround, the service account is used. When the service account is used, you can get and put values for Google Spreadsheet without logging in Google account using the gspread.
要使用服务帐户访问Google Spreadsheet,请执行以下流程.
In order to access to Google Spreadsheet with the service account, please do the following flow.
示例脚本:
将 sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0')脚本与服务帐户一起使用的示例脚本如下所示.
Sample script:
The sample script for using the script of sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0')
with the service account is as follows.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('### downloaded JSON file ###', scope)
gc = gspread.authorize(credentials)
sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0')
- 请设置
###下载的JSON文件###
.
这篇关于在python中,是否可以编辑公共共享的Google工作表,而无需使用身份验证代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!