Okay Nig42maa, your code was so useful! I'm few steps closer to finish my project. Refresh token is still killing my website, and the refresh function is not working. I paste my whole Python (Bottle) code here, and if someone can debug it, i really appreciate it. Thanks!
Code: Select all
from bottle import route, run, static_file, view, request, template
#from bottle import template as render_template
import requests
#import json
####### ####### ####### ####### ####### ####### #######
payload1 = {
'grant_type':'password',
'username':'xxx',
'password':'xxx',
'client_id':'xxx',
'client_secret':'xxx',
'scope':'read_station'
}
try:
response = requests.post("https://api.netatmo.com/oauth2/token", data=payload1)
response.raise_for_status()
access_token=response.json()["access_token"]
refresh_token=response.json()["refresh_token"]
scope=response.json()["scope"]
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
####### ####### ####### ####### ####### ####### #######
def refresh_login():
payload2 = {
'grant_type':'refresh_token',
'refresh_token':'xxx',
'client_id':'xxx',
'client_secret':'xxx'
}
try:
response = requests.post("https://api.netatmo.com/oauth2/token", data=payload2)
response.raise_for_status()
access_token=response.json()["access_token"]
expires_in=response.json()["expires_in"]
refresh_token=response.json()["refresh_token"]
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
####### ####### ####### ####### ####### ####### #######
payload3 = {
'access_token':response.json()["access_token"],
'device_id':'xxx'
}
####### ####### ####### ####### ####### ####### #######
@route('/static/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='./static')
@route('/netatmo')
#@view("/main.html")
def index():
try:
response = requests.post('https://api.netatmo.com/api/getstationsdata', data=payload3)
response.raise_for_status()
data = response.json()['body']
#print(json.dumps(data, indent=4))
xxx = data['devices'][0]['modules'][0]['dashboard_data']['Temperature']
xxx = data['devices'][0]['modules'][0]['dashboard_data']['min_temp']
xxx = data['devices'][0]['modules'][0]['dashboard_data']['max_temp']
xxx = data['devices'][0]['modules'][0]['dashboard_data']['Humidity']
xxx = data['devices'][0]['dashboard_data']['Temperature']
xxx = data['devices'][0]['dashboard_data']['min_temp']
xxx = data['devices'][0]['dashboard_data']['max_temp']
xxx = data['devices'][0]['dashboard_data']['Humidity']
xxx = data['devices'][0]['dashboard_data']['Noise']
if 'error' in response.json() and response.json()['error']['code'] in [2, 3] and retry:
refresh_login()
return index()
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
return template('main.html', xxx = xxx, xxx = xxx, xxx = xxx, xxx = xxx, xxx = xxx, xxx = xxx, xxx = xxx, xxx = xxx, xxx = xxx)
run(host='localhost', port=8080, debug=True)
All the x's are my personal data.