在此代码中:
import requests, pprint, re, gspread, time
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime
import oauth2client, httplib2
from oauth2client.file import Storage
def temperature():
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
storage = oauth2client.file.Storage('Singapore-Weather-84def2be176a.json')
credentials = storage.get()
http = httplib2.Http()
http = credentials.authorize(http)
credentials.refresh(http)
gc = gspread.authorize(credentials)
wks = gc.open('Singapore Weather').sheet1
r = requests.get('https://api.darksky.net/forecast/b02b5107a2c9c27deaa3bc1876bcee81/1.312914,%20103.780257')
json_object = r.json()
regexCurrentTemp = re.compile(r'"temperature":(\d\d.\d\d)')
moTemp = regexCurrentTemp.search(str(json_object))
temperature = moTemp.group(1)
regexApparentTemp = re.compile(r'"apparentTemperature":(\d\d.\d\d)')
moApparent = regexApparentTemp.search(str(json_object))
apparent = moApparent.group(1)
current = json_object['currently']
cloud = current['cloudCover']
cloud *= 100
timenow = datetime.now()
wks.append_row([str(timenow), temperature, apparent, cloud])
while True:
temperature()
time.sleep(3597)
我得到了一个与其中一个模块相关的错误代码,我不知道这是什么意思。错误是:
Traceback (most recent call last):
File "/Users/rosen59250/PycharmProjects/MorningWeather/main.py", line 39, in <module>
temperature()
File "/Users/rosen59250/PycharmProjects/MorningWeather/main.py", line 12, in temperature
credentials = storage.get()
File "/Users/rosen59250/EvenorOdd/lib/python3.7/site-packages/oauth2client/client.py", line 407, in get
return self.locked_get()
File "/Users/rosen59250/EvenorOdd/lib/python3.7/site-packages/oauth2client/file.py", line 54, in locked_get
credentials = client.Credentials.new_from_json(content)
File "/Users/rosen59250/EvenorOdd/lib/python3.7/site-packages/oauth2client/client.py", line 302, in new_from_json
module_name = data['_module']
KeyError: '_module'
为什么会出现这种错误代码?有没有办法解决这个问题,或者这是模块中的一个错误
如果我能修好它,我怎么能修好呢
出现此错误是因为您没有使用}的{a1}表示:
oauth2client
所期望的json格式。{这里的关键是,它希望您给它的json对象是它作为类的序列化实例生成的对象。您不能只给它任何arrbitrary json对象,然后期望它知道该做什么
当您从modules
to_json
方法生成JSON序列化凭证对象时,它将设置_module
和_class
数据这一点很重要的原因是,当您稍后尝试执行
new_from_json
时,它将执行这部分代码在您的情况下,这会失败,因为您使用的json不是模块生成的json凭证对象。这一点很重要的原因是,在代码的后面,它需要知道要返回给您的服务帐户对象
长话短说,你不能给它任何旧的json并期望它工作。你需要首先在类中创建你的凭证对象,然后调用它的to_json方法,然后序列化该对象,以便以后能够使用它从存储中加载凭证
您的数据目录中没有“_module”密钥。使用json身份验证对我有效:
然后,客户端可以打开工作表、追加行等。 希望这有帮助
相关问题 更多 >
编程相关推荐