Python 中 ServiceAccountCredentials.from_json_keyfile_name 访问 gpread 凭据的文件位置应该是什么

问题描述 投票:0回答:3

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 ;)")`
python file reference gspread
3个回答
0
投票

我解决了上述问题。

新问题是

gspread.exceptions.APIError: {
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"  }
}

我已与凭据中提供的电子邮件共享电子表格。


0
投票

尝试将范围更改为:

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

-2
投票
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'

-> 你是怎么解决这个问题的?

© www.soinside.com 2019 - 2024. All rights reserved.