首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我们如何从Google中读取Python中的DF?

我们如何从Google中读取Python中的DF?
EN

Stack Overflow用户
提问于 2020-03-09 02:16:54
回答 2查看 1.2K关注 0票数 1

我正在运行这段代码(完全相同)。

代码语言:javascript
运行
复制
import gspread
from oauth2client.service_account import ServiceAccountCredentials


# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Sheet1").sheet1

# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

我不完全确定这句话是否正确:

代码语言:javascript
运行
复制
scope = ['https://spreadsheets.google.com/feeds']

不管怎么说,我遵循了这个链接的说明。

https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

我点击了‘我的项目’>‘服务帐户’>‘启用’.我下载了JSON文件,命名为“client_secret.json”,并将其放在这个目录中:‘C:\Users\ryans\client_Incre.json’。最后,我打开json文件,得到“client_email in client_secret.json”并将其放入“共享”中,然后单击“Save”按钮。现在,当我运行上面的脚本时,我会得到以下错误消息:

代码语言:javascript
运行
复制
Traceback (most recent call last):

  File "<ipython-input-20-870ca6cceea6>", line 12, in <module>
    sheet = client.open("Sheet1").sheet1

  File "C:\Users\ryans\Anaconda3\lib\site-packages\gspread\client.py", line 123, in open
    self.list_spreadsheet_files()

  File "C:\Users\ryans\Anaconda3\lib\site-packages\gspread\client.py", line 96, in list_spreadsheet_files
    res = self.request('get', url, params=params).json()

  File "C:\Users\ryans\Anaconda3\lib\site-packages\gspread\client.py", line 79, in request
    raise APIError(response)

APIError: {'errors': [{'domain': 'global', 'reason': 'insufficientPermissions', 'message': 'Insufficient Permission: Request had insufficient authentication scopes.'}], 'code': 403, 'message': 'Insufficient Permission: Request had insufficient authentication scopes.'}

我不知道这里出了什么问题。会不会是权限问题?

更新

我增加了这一行:

代码语言:javascript
运行
复制
scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.readonly']

我的谷歌页面看起来如下:

现在,我将代码更改为:

代码语言:javascript
运行
复制
import os
import gspread
from oauth2client.service_account import ServiceAccountCredentials

# make sure the 'client_secret.json' is getting picked up...
os.getcwd()

# use creds to create a client to interact with the Google Drive API
scope = ['https://docs.google.com/spreadsheets/d/1PBB1eJ7zbcLyj7vsdrB8nEyZ9Ri0Nds8M2yFB0zEN1Q/edit#gid=0']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Sheet1").sheet1

# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

当我试图运行该脚本时,我会得到以下错误消息。

代码语言:javascript
运行
复制
Traceback (most recent call last):

  File "<ipython-input-34-e695bcd89439>", line 10, in <module>
    client = gspread.authorize(creds)

  File "C:\Users\ryans\Anaconda3\lib\site-packages\gspread\__init__.py", line 38, in authorize
    client.login()

  File "C:\Users\ryans\Anaconda3\lib\site-packages\gspread\client.py", line 51, in login
    self.auth.refresh(http)

  File "C:\Users\ryans\Anaconda3\lib\site-packages\oauth2client\client.py", line 545, in refresh
    self._refresh(http)

  File "C:\Users\ryans\Anaconda3\lib\site-packages\oauth2client\client.py", line 749, in _refresh
    self._do_refresh_request(http)

  File "C:\Users\ryans\Anaconda3\lib\site-packages\oauth2client\client.py", line 819, in _do_refresh_request
    raise HttpAccessTokenRefreshError(error_msg, status=resp.status)

HttpAccessTokenRefreshError: invalid_scope: Invalid oauth scope or ID token audience provided.

我的“”和“”都已启用。我遵循下面链接中列出的7个步骤。

https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

最后,我点击了Google页面上的“共享”。不过,正如上面所述,我仍然会遇到这个奇怪的错误。

注:这是我的谷歌表格的图片。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-09 02:50:19

这个答案怎么样?请把这看作是几个可能的答案之一。

问题和解决办法:

当我看到script脚本时,似乎在运行open("Sheet1")时,使用了Drive的files.list方法。参考在您的脚本中,只有https://spreadsheets.google.com/feeds用于作用域。我认为这样,insufficientPermissions的错误就发生了。为了避免这种情况,下面的修改如何?

修改脚本:

发自:

代码语言:javascript
运行
复制
scope = ['https://spreadsheets.google.com/feeds']

至:

代码语言:javascript
运行
复制
scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.readonly']
  • https://spreadsheets.google.com/feeds是用于Sheets API v3的。工作表v3 API将于2020年9月30日关闭。因此,在本例中,我建议使用https://www.googleapis.com/auth/spreadsheets作为使用Sheets的范围。另外,gspread使用了Sheets API v4。

注意:

  • 如果要使用修改文件元数据的方法,请使用https://www.googleapis.com/auth/drive的作用域而不是https://www.googleapis.com/auth/drive.readonly

参考文献:

票数 2
EN

Stack Overflow用户

发布于 2020-04-22 13:06:12

(代表问题作者发布解决方案,将其从提问栏移出)。

我终于弄明白这是怎么回事了。在名为'statup_funding.json‘的文件中,您必须获取生成的电子邮件,然后单击电子表格上的“共享”按钮,并将该电子邮件地址粘贴到打开的窗口中。然后,一切都如预期的那样运作。

这是我现在正在工作的代码的最后版本。

代码语言:javascript
运行
复制
import gspread
#Service client credential from oauth2client
from oauth2client.service_account import ServiceAccountCredentials
# Print nicely
import pprint
#Create scope
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
#create some credential using that scope and content of startup_funding.json
creds = ServiceAccountCredentials.from_json_keyfile_name('startup_funding.json',scope)
#create gspread authorize using that credential
client = gspread.authorize(creds)
#Now will can access our google sheets we call client.open on StartupName
sheet = client.open('Test_Sheet').sheet1
pp = pprint.PrettyPrinter()
results = sheet.get_all_records()
results
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60593979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档