Netatmo stationdata and refresh token issue

The Netatmo API is a set of webservices allowing developers and third parties to access Netatmo device's data.
avm067s
Posts: 9
Joined: 24 Mar 2019, 13:29

Netatmo stationdata and refresh token issue

Post by avm067s »

Hello developers!

I have done a working smart mirror project with two Netatmo modules (indoor and outdoor). I used Python 2.7.15 and Bottle in my project.

I have an issue with my refresh token. I can read station data succesfully, but my smart mirror Python code stays alive only 10800 seconds = 3 hours (expires_in). Could you please help me with a refresh token and how to use it a proper way? I already tried a "while True"-loop, but it has the same problem.

I use my tokens like this:

Code: Select all

params = {
	'access_token':response.json()['access_token'],
	'device_id':'xx:xx:xx:xx:xx:xx'
}
If you want to debug my code, send me a private message.


Br,
Aleksi
gulivert
Posts: 18
Joined: 20 Apr 2018, 09:39

Re: Netatmo stationdata and refresh token issue

Post by gulivert »

You must refresh your access token with the refresh token. Do a new post request to API URL "/oauth2/token" with as parameters: client ID, client secret and grant_type set to "refresh_token". You must do that before the current access token expire (3hours of validity) and you will get a new access_token valid for 3 more hours...

The doc explain this clearly... Peek a look
avm067s
Posts: 9
Joined: 24 Mar 2019, 13:29

Re: Netatmo stationdata and refresh token issue

Post by avm067s »

I already have those lines and my access token is refreshed with a refresh token. The code example above is from refresh token's payload and it's parameters. The question is, how can I automatically update my Python-code (loop?) to get a new access token? My webpage dies after 3 hours. In html-file, there's an automatic refresh function.

I tried to feed my refresh_token response.json to params, like my example shows up.
Nig42maa
Posts: 4
Joined: 25 Mar 2019, 09:56

Re: Netatmo stationdata and refresh token issue

Post by Nig42maa »

If you have a loop in your code, checking time hasn't elapsed 3 hours is a good idea, you could then trigger your function that refresh your token.

However I advice an other way of doing it if you don't have a loop mechanism in your code :
Wrap your call to the api in a function that checks the return code of the request. If the return code is 401, then it means your token may have expired. Trigger the refresh and retry your request. If it fails again, it should fall in your error catching stack.

I find this way of refreshing token more elegant.
avm067s
Posts: 9
Joined: 24 Mar 2019, 13:29

Re: Netatmo stationdata and refresh token issue

Post by avm067s »

Why there's no good examples of doing that? My smart mirror project is 99% done and I'm stuck with this issue. Do you guys have any good examples how to solve this refreshing problem?
Nig42maa
Posts: 4
Joined: 25 Mar 2019, 09:56

Re: Netatmo stationdata and refresh token issue

Post by Nig42maa »

Here's how I do it if you're interested : https://gitlab.com/nimag42/loft-homescr ... Netatmo.py
avm067s
Posts: 9
Joined: 24 Mar 2019, 13:29

Re: Netatmo stationdata and refresh token issue

Post by avm067s »

404 - page not found. I also tried to find your project, but it seems to be empty.
Nig42maa
Posts: 4
Joined: 25 Mar 2019, 09:56

Re: Netatmo stationdata and refresh token issue

Post by Nig42maa »

Hmm sorry permissions was too hard, you can retry
avm067s
Posts: 9
Joined: 24 Mar 2019, 13:29

Re: Netatmo stationdata and refresh token issue

Post by avm067s »

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.
gulivert
Posts: 18
Joined: 20 Apr 2018, 09:39

Re: Netatmo stationdata and refresh token issue

Post by gulivert »

I'm not a specialist in Python and even not programmed anything with this language but it seems that you never enter to the refresh token function because you have not initialised retry variable to true.

May be something like this should help :

Code: Select all

def index(retry=true):
    	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(false)
Post Reply

Return to “Netatmo API”