【发布时间】:2020-03-11 15:27:25
【问题描述】:
我正在尝试运行以下代码来访问 Google 表格。
import os
os.getcwd()
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secret_406147946600-na19mdik9a4571623b4kdp30h5354el0.apps.googleusercontent.com.json', scope) # Your json file here
credentials
我从“OAuth 2.0 客户端 ID”中获得了 .json 文件。我所做的只是单击“下载 JSON”,然后将文件放在名为“C:\Users\ryans”的默认文件夹中。当我运行代码时,我得到了这个错误。
Traceback (most recent call last):
File "<ipython-input-17-26266215508f>", line 9, in <module>
credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secret_406147946600-na19mdik9a4571623b4kdp30h5354el0.apps.googleusercontent.com.json', scope) # Your json file here
File "C:\Users\ryans\Anaconda3\lib\site-packages\oauth2client\service_account.py", line 223, in from_json_keyfile_name
revoke_uri=revoke_uri)
File "C:\Users\ryans\Anaconda3\lib\site-packages\oauth2client\service_account.py", line 172, in _from_parsed_json_keyfile
'Expected', client.SERVICE_ACCOUNT)
ValueError: ('Unexpected credentials type', None, 'Expected', 'service_account')
我所做的只是给它提供与它给我完全相同的文件。就是这样。如果我将名称更改为:'service_account.json',则会收到以下错误。
ValueError: ('Unexpected credentials type', None, 'Expected', 'service_account')
我所做的正是它告诉我要做的。我不明白这里有什么问题。这似乎是一个 2 秒的工作(复制/粘贴),但这里根本没有任何工作。我在网上看到过其他示例,其他人表示一切正常,但无论我做什么,都对我不起作用。是否有某种特殊的安全设置或类似的东西?我可以禁用一些安全性(如果它存在的话)吗?
【问题讨论】:
标签: python python-3.x google-sheets google-sheets-api