我正在尝试学习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(对不起,全部用日语)。
这家伙提出的方法是使用名为p12的键作为键(?),以便在创建OAuth2凭据时访问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">シート1</ns0:title>0
但它给我这个字符串/符号错误。
我做错了吗?或者是否在此get_worksheets命令上更改了API?我知道自从编写本教程以来,很多事情都可能发生了变化,因为我必须更改&#34; import SignedJwtAssertionCredentials&#34;到&#34;导入ServiceAccountCredentials&#34;因为显然SignedJwtAssertionCredentials不再存在(根据这个帖子:https://github.com/google/oauth2client/issues/401)
我知道我可以使用其他更简单的&#34; gspread&#34; API,但我的理解是,您无法使用它实际编辑电子表格,因此我只使用gdata。
提前非常感谢你。
答案 0 :(得分:1)
谷歌推出了v4 api的电子表格,其中包含一篮子选项。因为我发现很难使用谷歌客户端库,我在google 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')