使用Python客户端库读取私有谷歌表格

2 投票
2 回答
1318 浏览
提问于 2025-04-30 05:07

我有一个谷歌表格,想通过谷歌的Python客户端来读取这个表格。这个表格是私密的。下面是我的代码:

import gdata  
import gdata.docs  
import gdata.spreadsheet  
import gdata.spreadsheet.service  
client = gdata.spreadsheet.service.SpreadsheetsService()  
client.email = 'xxx.xxx@companyName.com'  
client.password = 'yyyyy'  
client.ProgrammaticLogin()  
spreadsheet_feed = client.GetFeed('http://spreadsheets.google.com/feeds/spreadsheets/private/full')  
first_entry = spreadsheet_feed.entry[0]  
key = first_entry.id.text.rsplit('/')[-1]  
worksheets_feed = client.GetWorksheetsFeed(key)  
for entry in worksheets_feed.entry:  
     print entry.title.text  

运行这段代码时,我遇到了以下错误:

    client.ProgrammaticLogin()
  File "C:\python27\lib\site-packages\gdata\service.py", line 793, in ProgrammaticLogin
    raise BadAuthentication, 'Incorrect username or password'
gdata.service.BadAuthentication: Incorrect username or password

为什么即使用户名和密码都是正确的,而且用户名也在公司域名下,但还是在谷歌服务器上出问题呢?

暂无标签

2 个回答

1

你可以为你的程序启用一个“应用专用密码”。这个密码只会在你正在使用的程序中用到。谷歌的具体说明可以在这里找到:https://support.google.com/accounts/answer/185833?rd=1,简单来说,你需要去https://security.google.com/settings/security/apppasswords创建一个新的密码。

1

你遇到的问题是因为谷歌的安全协议。谷歌不允许一些安全性较低的应用程序连接到你的账户,而这些gdata API使用的安全措施比较低。因此,如果你想用这些API连接到你的账户,就需要允许安全性较低的应用访问你的账户。你可以去这个链接设置一下:https://www.google.com/settings/security/lesssecureapps

撰写回答