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

基于DS18B20的树莓派温度日志程序OAuth模块缺失问题求助

解决DS18B20温度记录器连接Google Docs的OAuth过时问题

你的问题核心是2015年出版的书中使用的OAuth认证方式早已被Google淘汰,gaugette.oauth库要么停止维护,要么不支持现在的Google API认证规则,所以才会出现AttributeError: 'module' object has no attribute 'OAuth'的报错。下面是适配当前Google Sheets API的完整解决方案:

一、替换依赖库,用官方认证方式

放弃原来的gaugette.oauth,改用Google官方支持的工具库来操作表格:

pip install gspread oauth2client

二、创建Google Cloud服务账号(获取JSON密钥)

这是现在Google API认证的标准方式,不需要手动获取验证码:

  • 登录Google Cloud控制台,创建一个新项目
  • 在项目中启用Google Sheets API和Google Drive API
  • 创建一个服务账号,下载对应的JSON密钥文件(保存到树莓派的项目目录,比如命名为service_account_key.json)
  • 打开你的Google Sheets温度日志表格,把服务账号的邮箱地址(在JSON密钥文件里的client_email字段)添加为表格的编辑者

三、重写日志函数,替换旧的gdocs.py

删掉原来的gdocs.py,新建一个包含以下代码的gdocs.py:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime

def LogRowInGDocSpreadsheet(spreadsheet_key, headings, temperature, unit_of_measure):
    # 定义API访问范围
    scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
    # 加载JSON密钥完成认证
    creds = ServiceAccountCredentials.from_json_keyfile_name('service_account_key.json', scope)
    client = gspread.authorize(creds)
    
    # 打开目标表格(默认操作第一个工作表)
    sheet = client.open_by_key(spreadsheet_key).sheet1
    
    # 自动写入表头(如果表头不存在)
    existing_headings = sheet.row_values(1)
    if existing_headings != headings:
        sheet.insert_row(headings, 1)
    
    # 准备要记录的数据
    current_datetime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    log_row = [temperature, current_datetime, unit_of_measure]
    
    # 追加数据到表格末尾
    sheet.append_row(log_row)

四、修改templogger_gdocs.py的调用逻辑

原来的调用需要传client_id和client_secret,现在不需要了,把main函数里的调用改成:

# 先获取温度数据(保留你原来的GetTemperature逻辑)
temperature = GetTemperature(ds18b20_dir, fahrenheit)
# 调用新的日志函数
LogRowInGDocSpreadsheet(spreadsheet_key, headings, temperature, unit_of_measure)

关键说明

  • 旧代码依赖的OAuth 1.0a协议已经被Google废弃,现在必须使用OAuth 2.0,服务账号的JSON密钥是最适合无人值守设备(比如树莓派)的认证方式
  • 不需要手动输入验证码或token,认证流程由密钥文件自动完成
  • 确保你的树莓派能正常访问Google的API服务,检查网络连接和防火墙设置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:15:52