You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python脚本报AttributeError: str无sign属性,求排查解决(Python3.6)

解决AttributeError: 'str' object has no attribute 'sign'问题

这个错误的核心原因很明确:你传给ServiceAccountCredentials的key参数是原始字符串,但这个方法需要的是一个包含密钥信息的字典(或者可处理的密钥对象),字符串自然没有sign方法,所以触发了这个报错。下面分步骤帮你解决:

1. 先修复JSON解析问题

你当前直接读取了JSON文件的文本内容,但没有把它解析成Python字典。修改代码如下:

import json
from oauth2client.service_account import ServiceAccountCredentials

# 用with语句自动管理文件,同时解析JSON为字典
with open(r'c:\ExamCreator\My Project-9352ed298182.json', 'r') as f:
    key = json.load(f)

credentials = ServiceAccountCredentials(
    'CLIENT_EMAIL',
    key,
    scope='https://www.googleapis.com/auth/admin.directory.user',
    sub='amankahlon@tes...'
)

注意:路径用原始字符串r'...'避免转义符问题,或者把反斜杠写成\\。

2. 检查依赖库与Python版本兼容性

你用的Python 3.6已经停止维护了,不过如果坚持使用,要确保依赖库版本兼容:

  • 如果你用的是oauth2client库,要安装适合Python 3.6的版本,比如:
    pip install oauth2client==4.1.3
    
  • 更推荐的做法是迁移到官方推荐的google-auth库(oauth2client已被弃用),它对新Python版本支持更好,用法也更简洁:
    from google.oauth2 import service_account
    
    credentials = service_account.Credentials.from_service_account_file(
        r'c:\ExamCreator\My Project-9352ed298182.json',
        scopes=['https://www.googleapis.com/auth/admin.directory.user'],
        subject='amankahlon@tes...'
    )
    
    安装google-auth的命令:
    pip install google-auth
    

3. 额外检查点

  • 确认你的JSON密钥文件路径正确,文件没有损坏,内容包含private_key等必要字段。
  • 确保CLIENT_EMAIL的值和JSON文件里的client_email字段一致。

内容的提问来源于stack exchange,提问作者user9201220

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:54:36