Python 3.x 使用python 3连接Google Spreadhseet

Python 3.x 使用python 3连接Google Spreadhseet,python-3.x,google-sheets,google-sheets-api,Python 3.x,Google Sheets,Google Sheets Api,我正在使用以下代码连接到google电子表格 import gspread from oauth2client.service_account import ServiceAccountCredentials scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'] credentials = ServiceAccountCredentials.fro

我正在使用以下代码连接到google电子表格

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)

gc = gspread.authorize(credentials)
我有从GoogleSheetsAPI控制台导出的credentials.json

但我得到了以下错误:

TypeError: cannot use a string pattern on a bytes-like object
这是错误的完整跟踪:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-ee731769ce7a> in <module>()
      7 credentials = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
      8 
----> 9 gc = gspread.authorize(credentials)

~\Anaconda3\lib\site-packages\gspread\__init__.py in authorize(credentials, client_class)
     36     """
     37     client = client_class(auth=credentials)
---> 38     client.login()
     39     return client

~\Anaconda3\lib\site-packages\gspread\client.py in login(self)
     49 
     50             http = httplib2.Http()
---> 51             self.auth.refresh(http)
     52 
     53         self.session.headers.update({

c:\users\developer\appdata\local\google\cloud sdk\google-cloud-sdk\lib\third_party\oauth2client\client.py in refresh(self, http)
    570                   request.
    571         """
--> 572         self._refresh(http.request)
    573 
    574     def revoke(self, http):

c:\users\developer\appdata\local\google\cloud sdk\google-cloud-sdk\lib\third_party\oauth2client\client.py in _refresh(self, http_request)
    780         """
    781         if not self.store:
--> 782             self._do_refresh_request(http_request)
    783         else:
    784             self.store.acquire_lock()

c:\users\developer\appdata\local\google\cloud sdk\google-cloud-sdk\lib\third_party\oauth2client\client.py in _do_refresh_request(self, http_request, attempt)
    816         resp, content = http_request(
    817             self.token_uri.encode('idna'), method='POST',
--> 818             body=body, headers=headers)
    819         content = _helpers._from_bytes(content)
    820         if resp.status == http_client.OK:

c:\users\developer\appdata\local\google\cloud sdk\google-cloud-sdk\lib\third_party\httplib2\python3\httplib2\__init__.py in request(self, uri, method, body, headers, redirections, connection_type)
   1767             uri = iri2uri(uri)
   1768 
-> 1769             (scheme, authority, request_uri, defrag_uri) = urlnorm(uri)
   1770 
   1771             conn_key = scheme + ":" + authority

c:\users\developer\appdata\local\google\cloud sdk\google-cloud-sdk\lib\third_party\httplib2\python3\httplib2\__init__.py in urlnorm(uri)
    218 
    219 def urlnorm(uri):
--> 220     (scheme, authority, path, query, fragment) = parse_uri(uri)
    221     if not scheme or not authority:
    222         raise RelativeURIError("Only absolute URIs are allowed. uri = %s" % uri)

c:\users\developer\appdata\local\google\cloud sdk\google-cloud-sdk\lib\third_party\httplib2\python3\httplib2\__init__.py in parse_uri(uri)
    213         (scheme, authority, path, query, fragment) = parse_uri(uri)
    214     """
--> 215     groups = URI.match(uri).groups()
    216     return (groups[1], groups[3], groups[4], groups[6], groups[8])
    217 

TypeError: cannot use a string pattern on a bytes-like object
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
7 credentials=ServiceAccountCredentials.from_json_keyfile_name(“credentials.json”,scope)
8.
---->9 gc=gspread.authorize(凭证)
~\Anaconda3\lib\site packages\gspread\\uuuuu init\uuuuuuuuuuuu.py在授权中(凭据,客户端类)
36     """
37客户端=客户端\u类(身份验证=凭据)
--->38 client.login()
39退货客户
登录时使用~\Anaconda3\lib\site packages\gspread\client.py(self)
49
50 http=httplib2.http()
--->51自我验证刷新(http)
52
53 self.session.headers.update({
刷新中的c:\users\developer\appdata\local\google\cloud sdk\google cloud sdk\lib\third\u party\oauth2client\client.py(self,http)
570请求。
571         """
-->572自我刷新(http.request)
573
574 def revoke(自我,http):
c:\users\developer\appdata\local\google\cloud sdk\google cloud sdk\lib\third\u party\oauth2client\client.py in\u刷新(self,http\u请求)
780         """
781如果不是自助商店:
-->782自我更新请求(http请求)
783其他:
784 self.store.acquire_lock()
c:\users\developer\appdata\local\google\cloud sdk\google cloud sdk\lib\third\u party\oauth2client\client.py in\u do\u refresh\u请求(self、http\u请求、尝试)
816 resp,content=http\u请求(
817 self.token_uri.encode('idna'),method='POST',
-->818正文=正文,标题=标题)
819 content=\u helpers.\u from\u字节(content)
820如果resp.status==http_client.OK:
c:\users\developer\appdata\local\google\cloud sdk\google cloud sdk\lib\third\uu party\httplib2\python3\httplib2\\uuuuuuu init\uuuuuu.py请求中(self、uri、方法、主体、头、重定向、连接类型)
1767 uri=iri2uri(uri)
1768
->1769(方案、权限、请求uri、碎片整理uri)=urlnorm(uri)
1770
1771 conn_key=方案+“:”+权限
urlnorm(uri)中的c:\users\developer\appdata\local\google\cloud sdk\google cloud sdk\lib\third\uu party\httplib2\python3\httplib2\\ uuuuuuuuuuuuuuuuuuuuuu init.py
218
219定义urlnorm(uri):
-->220(方案、权限、路径、查询、片段)=解析uri(uri)
221如果不是计划或非授权:
222相对uri错误(“仅允许绝对uri。uri=%s”%uri)
c:\users\developer\appdata\local\google\cloud sdk\google cloud sdk\lib\third\httplib2\python3\httplib2\\解析uri(uri)中的init\uuuuuuuuuuuuuu.py
213(方案、权限、路径、查询、片段)=解析uri(uri)
214     """
-->215 groups=URI.match(URI.groups)()
216返回(组[1]、组[3]、组[4]、组[6]、组[8])
217
TypeError:无法在类似字节的对象上使用字符串模式

从错误消息中,
credentials.json
可能无法用于服务帐户。因此,我可以向您询问我从Google Sheets API控制台导出的
credentials.json。
?该json文件具有以下键:类型、项目id、私钥id、私钥、客户端电子邮件、客户端id、身份验证uri、token_uri、身份验证提供程序\u x509\u证书\u url、客户端\u x509\u证书\u url。所有json文件的格式为“key”:“value”。感谢您的回复。从您的回复来看,该文件似乎是针对服务帐户的。在这种情况下,如何检查该文件是否为UTF-8?在我的环境中,当服务帐户的文件另存为UTF-8时,我可以确认您的脚本是否工作。但我不确定这是否是您问题的直接解决方案。这是d由于我的拙劣技能。对此我深表歉意。对凭据进行解码如何;
gc=gspread.authorize(credentials.decode('utf-8'))
这会导致以下错误:---------------------------------------------------------------------------------------------------------------AttributeError回溯(最近的一次呼叫最后一次)in()--->1 gc=gspread.authorize(credentials.decode('utf-8'))AttributeError:'ServiceAccountCredentials'对象没有属性'decode'