[英]Q about scope value when using gspread API to read google sheet with Python
I am new to google apis.我是 google api 的新手。 I am looking to read a google spreadsheet via a python program and following instructions found here:
我希望通过 python 程序阅读谷歌电子表格并按照此处的说明进行操作:
https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html
I have a google sheet called Legislators 2017 that I am using for this.我有一个名为 Legislators 2017 的谷歌表格,我正在使用它。
The following is the code to print out some content from the sheet.以下是从工作表中打印出一些内容的代码。
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('google_drive_oct_6_2020.json', scope)
client = gspread.authorize(creds)
sheet = client.open("Legislators 2017").sheet1
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)
The above works.以上作品。
I am inclined to use the smallest scope possible in general, and would like to use一般来说,我倾向于使用最小的 scope,并且想使用
https://www.googleapis.com/auth/spreadsheets or
https://www.googleapis.com/auth/spreadsheets.readonly
but they don't work and I get an exception with the following message:但它们不起作用,我收到以下消息的异常:
Insufficient Permission: Request had insufficient authentication scopes.
What am I missing?我错过了什么?
When client.open("Legislators 2017")
is used, the method of "Files: list" in Drive API is used.使用
client.open("Legislators 2017")
时,使用Drive API中"Files: list"的方法。 Ref By this, the scope of Drive API is required to be used. Ref至此,需要使用Drive API的scope。 In your script,
https://www.googleapis.com/auth/drive
or https://www.googleapis.com/auth/drive.readonly
are required.在您的脚本中,
https://www.googleapis.com/auth/drive
或https://www.googleapis.com/auth/drive.readonly
。
When you don't want to use the scopes of Drive API and want to use the smallest scope possible in general
, how about the following modification?当你不想使用 Drive API 的作用域而想使用
the smallest scope possible in general
,如何进行以下修改?
scope = ['https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('google_drive_oct_6_2020.json', scope)
client = gspread.authorize(creds)
sheet = client.open("Legislators 2017").sheet1
scope = ['https://www.googleapis.com/auth/spreadsheets.readonly']
creds = ServiceAccountCredentials.from_json_keyfile_name('google_drive_oct_6_2020.json', scope)
client = gspread.authorize(creds)
sheet = client.open_by_key("###").sheet1
###
is the Spreadsheet ID of Legislators 2017
. ###
是Legislators 2017
的电子表格 ID。https://www.googleapis.com/auth/spreadsheets.readonly
can be used.https://www.googleapis.com/auth/spreadsheets.readonly
的scope。 Also, https://www.googleapis.com/auth/spreadsheets
can be used.https://www.googleapis.com/auth/spreadsheets
。When creating the Authentication credentials from GCP, you need to state the level of access given to a Authentication Key.从 GCP 创建身份验证凭据时,您需要 state 授予身份验证密钥的访问级别。 You can then access that level of access, or less.
然后,您可以访问该级别或更少的访问权限。
According to the guide you are referring:根据您所指的指南:
Name the service account and grant it a Project Role of Editor.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.