ServiceAccountCredentials添加作用域报错:list[str]无法赋值给str参数
解决oauth2client中scopes参数类型不匹配错误
你遇到的错误提示说明ServiceAccountCredentials.from_json_keyfile_name方法需要接收字符串类型的scopes参数,但你传入了字符串列表。虽然网上多数示例用列表传参,但可能是版本差异或类型检查工具的问题,试试下面的解决办法:
将scopes列表转为空格分隔的字符串
修改你的scope变量为字符串形式,直接传入方法:from oauth2client.service_account import ServiceAccountCredentials credentials_file = 'creds.json' # 转为空格分隔的字符串 scope = 'https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive' credentials = ServiceAccountCredentials.from_json_keyfile_name( credentials_file, scope)检查并调整oauth2client版本
运行以下命令查看当前oauth2client版本:pip show oauth2client若版本较新仍报错,尝试降级到2.2.0版本:
pip install oauth2client==2.2.0迁移到官方推荐的新库(优先推荐)
oauth2client已被Google弃用,官方现在推荐使用google-auth系列库,兼容性更好,示例代码如下:from google.oauth2 import service_account credentials_file = 'creds.json' scopes = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'] credentials = service_account.Credentials.from_service_account_file( credentials_file, scopes=scopes)先安装所需依赖:
pip install google-auth google-auth-oauthlib
内容的提问来源于stack exchange,提问作者Никита Тен
相关产品推荐
相关产品推荐

