简体   繁体   English

Q 关于使用 gspread API 读取 Python 时的 scope 值

[英]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/drivehttps://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 ,如何进行以下修改?

From:从:

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

To:到:

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。
  • In this modification, the scope of 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

References:参考:

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.

相关问题 python gspread-如何删除/删除google-sheet-API中的列 - python gspread - How to delete/remove column in google-sheet-API 如何在 python 中使用 gspread 添加到 google 工作表单元格的超链接? - How to add hyperlink to a google sheet cell using gspread in python? 使用 Gspread 和 Python 从 google sheet 获取所有列值 - Getting all column values from google sheet using Gspread and Python 打开工作表时出现Python3 gspread错误 - Python3 gspread error when opening sheet 有没有办法使用 Python 的 gspread API 自动调整 Google 表格中的列? - Is there a way to autofit a column in Google Sheets using gspread API for Python? 使用gspread和python时如何链接Google帐户? - How to link google account when using gspread and python? 如何使用python GSPREAD找到谷歌电子表格的第一行空行? - How to find the first empty row of a google spread sheet using python GSPREAD? 使用 google sheet API 和 gspread 编写列的最快方法 - Quickest way to write a column with google sheet API and gspread 如何使用 gspread 和 gspread_formatting 创建具有 Python 值的复选框 - How to create a checkbox with value with Python using gspread and gspread_formatting Python gspread,如何填充谷歌表,列表中的字典 - Python gspread, how to populate google sheet, dictionaries within list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM