python - 无法访问谷歌电子表格

标签 python google-apps-script google-api google-sheets google-sheets-api

我正在尝试学习 Google 的 gdata Python API,并尝试初始访问我的驱动器中的测试电子表格。这是我用作测试的代码:

from oauth2client.service_account import ServiceAccountCredentials
import gdata.spreadsheets.client

# 認証に必要な情報
client_email = "59508263769-compute@developer.gserviceaccount.com" # 手順2で発行されたメールアドレス
with open("C:\Users\User2\Downloads\My Project-e7c4abb055f3.p12") as f: private_key = f.read() # 手順2で発行された秘密鍵

# 認証情報の作成
scope = ["https://spreadsheets.google.com/feeds"]
credentials = ServiceAccountCredentials(client_email, private_key,
    scope=scope)

# スプレッドシート用クライアントの準備
client = gdata.spreadsheets.client.SpreadsheetsClient()

# OAuth2.0での認証設定
auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
auth_token.authorize(client)

# ---- これでライブラリを利用してスプレッドシートにアクセスできる ---- #

# ワークシートの取得
sheets = client.get_worksheets("1RRajoMlKP2plSnE12dt_BqVbk9TihCpDpumvhF-y7vM") # スプレッドシートIDを指定
for sheet in sheets.entry:
    print sheet.get_worksheet_id(), sheet.title

我基于本教程 http://qiita.com/koyopro/items/d8d56f69f863f07e9378 (抱歉,全部是日语)。

此人提出的方法是在创建 OAuth2 凭据时使用 p12 作为 key (?)来访问 Google Apps 中的内容。然后将您的电子表格共享到生成的 @developer 电子邮件地址。

我运行这段代码,一切都很好,直到它命中

sheets = client.get_worksheets("1RRajoMlKP2plSnE12dt_BqVbk9TihCpDpumvhF-y7vM") # スプレッドシートIDを指定

这给了我以下错误:

Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
========= RESTART: C:\Users\User2\Documents\googlePythonTest04_01.py =========

Traceback (most recent call last):
  File "C:\Users\User2\Documents\googlePythonTest04_01.py", line 24, in <module>
    sheets = client.get_worksheets("1RRajoMlKP2plSnE12dt_BqVbk9TihCpDpumvhF-y7vM") # スプレッドシートIDを指定
  File "C:\Python27\lib\site-packages\gdata\spreadsheets\client.py", line 108, in get_worksheets
    **kwargs)
  File "C:\Python27\lib\site-packages\gdata\client.py", line 640, in get_feed
    **kwargs)
  File "C:\Python27\lib\site-packages\gdata\client.py", line 267, in request
    uri=uri, auth_token=auth_token, http_request=http_request, **kwargs)
  File "C:\Python27\lib\site-packages\atom\client.py", line 122, in request
    return self.http_client.request(http_request)
  File "C:\Python27\lib\site-packages\gdata\gauth.py", line 1332, in new_request
    refresh_response = self._refresh(request_orig)
  File "C:\Python27\lib\site-packages\gdata\gauth.py", line 1473, in _refresh
    self.credentials._refresh(httplib2.Http().request)
  File "C:\Python27\lib\site-packages\oauth2client-2.2.0-py2.7.egg\oauth2client\client.py", line 873, in _refresh
    self._do_refresh_request(http_request)
  File "C:\Python27\lib\site-packages\oauth2client-2.2.0-py2.7.egg\oauth2client\client.py", line 900, in _do_refresh_request
    body = self._generate_refresh_request_body()
  File "C:\Python27\lib\site-packages\oauth2client-2.2.0-py2.7.egg\oauth2client\client.py", line 1611, in _generate_refresh_request_body
    assertion = self._generate_assertion()
  File "C:\Python27\lib\site-packages\oauth2client-2.2.0-py2.7.egg\oauth2client\service_account.py", line 396, in _generate_assertion
    key_id=self._private_key_id)
  File "C:\Python27\lib\site-packages\oauth2client-2.2.0-py2.7.egg\oauth2client\crypt.py", line 97, in make_signed_jwt
    signature = signer.sign(signing_input)
AttributeError: 'str' object has no attribute 'sign'
>>> 

根据本教程,最后一个打印命令应该返回

od6 <ns0:title xmlns:ns0="http://www.w3.org/2005/Atom">&#12471;&#12540;&#12488;1</ns0:title>0

但是它给了我这个字符串/符号错误。

我做错了什么吗?或者这个 get_worksheets 命令的 API 是否发生了变化?我知道自本教程编写以来,很多事情可能已经发生了变化,因为我必须将“import SignedJwtAssertionCredentials”更改为“import ServiceAccountCredentials”,因为显然 SignedJwtAssertionCredentials 不再存在(根据此线程:https://github.com/google/oauth2client/issues/401)

我知道我可以使用其他更简单的“gspread”API,但我的理解是你实际上无法用它编辑电子表格,所以我只使用 gdata。

提前非常感谢您。

最佳答案

Google 推出了适用于电子表格的 v4 API,其中包含一篮子选项。因为我发现很难使用谷歌客户端库,所以我在谷歌Python客户端之上为谷歌电子表格编写了一个Python库(pygsheets)。该库正处于开发阶段,因此如果您发现缺少任何功能,请随时发送拉取请求或只是创建问题。如果您需要更稳定的库并且不需要新的 api checkout gspread是的,您可以使用 gspread 编辑电子表格。

使用方法很简单

import pygsheets

gc = pygsheets.authorize()

# Open a worksheet from spreadsheet with one shot
wks = gc.open('my new ssheet').sheet1

wks.update_acell('B2', "it's down there somewhere, let me take another look.")

# Fetch a cell range
cell_list = wks.range('A1:B7')

关于python - 无法访问谷歌电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37914136/

相关文章:

python - 在类中定义函数时如何获取参数?

javascript - 在这种情况下有没有比 eval() 更好的方法?

python - 如何使用 Python 启用 Google Assistant API 的新 "read it"功能?

java - 启动服务不起作用

javascript - 如何使用 CORS 实现 JavaScript Google Places API 请求

python - Python 的 3d 数学库

python - 索引 Python 打印语句

python - 将浮点范围放入容器中

google-apps-script - 使用我自己的 Google Docs 插件而不发布它

google-apps-script - 使用 Google Apps 脚本从基于 Google 表格的 Google 日历中删除事件