Error "Invalid access_token" on Energy homestatus API

The Netatmo API is a set of webservices allowing developers and third parties to access Netatmo device's data.
Post Reply
sadmin91
Posts: 4
Joined: 28 Dec 2015, 16:09

Error "Invalid access_token" on Energy homestatus API

Post by sadmin91 » 07 Mar 2018, 21:04

Hello,

I'm trying to call the new homestatus API on a python script.
But I receive this error message : "Invalid access_token"

With the same code, I have call the homesdata API without problem.

Can you help me?

Thanks

My code :

Code: Select all

payload = {'grant_type': 'password',
	       'username': "xxxx",
	       'password': "xxxx",
	       'client_id':"xxxx",
	       'client_secret': "xxxx",
	       'scope': 'read_thermostat'}
try:
	response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
	response.raise_for_status()
	access_token = response.json()["access_token"]
except requests.exceptions.HTTPError as error:
	print(error.response.status_code, error.response.text)
	
paramsAPI = {
    'access_token': access_token,
    'home_id': "xxxx"
}
   
try:
    response = requests.post("https://api.netatmo.com/api/homestatus", params=paramsAPI)
    response.raise_for_status()
    data = response.json()["body"]
except requests.exceptions.HTTPError as error:
    print(error.response.status_code, error.response.text)

Céline
Posts: 37
Joined: 20 Nov 2017, 18:17

Re: Error "Invalid access_token" on Energy homestatus API

Post by Céline » 08 Mar 2018, 10:02

Hi,

You need to use bearer authentification. To do so you need to include the token in the header of the request :

hed = {'Authorization': 'Bearer ' + access_token}
response = requests.post("https://api.netatmo.com/oauth2/token", data=payload,headers=hed)
Céline - Netatmo Team :D

sadmin91
Posts: 4
Joined: 28 Dec 2015, 16:09

Re: Error "Invalid access_token" on Energy homestatus API

Post by sadmin91 » 08 Mar 2018, 20:27

I have tryed with adding an header on request but the same error occurs.
Below my new code :

Code: Select all

payload = {'grant_type': 'password',
          'username': "xxxx",
          'password': "xxxx",
          'client_id':"xxxx",
          'client_secret': "xxxx",
          'scope': 'read_thermostat'}
try:
   response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
   response.raise_for_status()
   access_token = response.json()["access_token"]
except requests.exceptions.HTTPError as error:
   print(error.response.status_code, error.response.text)
   
paramsAPI = {
    'access_token': access_token,
    'home_id': "xxxx"
}
hedAPI = {'Authorization': 'Bearer ' + access_token}
   
try:
    response = requests.post("https://api.netatmo.com/api/homestatus", params=paramsAPI, headers=hedAPI)
    response.raise_for_status()
    data = response.json()["body"]
except requests.exceptions.HTTPError as error:
    print(error.response.status_code, error.response.text)
I am surprised with this solution because when I try to call the Homesdata API without an header, I don't receive this error.

Céline
Posts: 37
Joined: 20 Nov 2017, 18:17

Re: Error "Invalid access_token" on Energy homestatus API

Post by Céline » 09 Mar 2018, 12:09

Hi,
You need to remove the access_token from the param as it is now in the header. That should work after that.
Putting the token in the header in more secured and that is why the older way is no longer supported in the new endpoints.
Céline - Netatmo Team :D

Post Reply

Return to “Netatmo API”