ServiceAccountCredentials.from_json_keyfile_name 访问 gpread 凭据的 python 中的文件位置应该是什么?
这是我的代码:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('PythonGspread-c6fb2247fb25.json', scope)
client = gspread.authorize(creds)
# Find a workbook by name and open the first sheet
sheet = client.open("Python").sheet1
# Make sure you use the right name here.
# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
'result = sheet.cell(2,1).value'
print(list_of_hashes)
print("\n")
print(result)
input("Press enter to exit ;)")`
我解决了上述问题。
新问题是
gspread.exceptions.APIError: {
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission" }
}
我已与凭据中提供的电子邮件共享电子表格。
尝试将范围更改为:
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('PythonGspread-
c6fb2247fb25.json', scope) File "C:\Users\Krishna\Anaconda3\lib\site-
packages\oauth2client\service_account.py", line 219, in
from_json_keyfile_name with open(filename, 'r') as file_obj:
FileNotFoundError: [Errno 2] No such file or directory:
'PythonGspread-c6fb2247fb25.json'
-> 你是怎么解决这个问题的?