Refresh token - Invalid grant_type parameter or parameter missing
Hello,
I'm trying to manage my Bubendorf rollers from a Tasmota Berry script and with curl
When using the token I generated in the Netatmo app, it works fine (I can for instance open/close a shutter), both with Berry and with curl,...but during 3 hours only.
When trying to refresh the token (before the access token has expired), I always get this error : {"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
Here is the curl request (XXXXXXXX is the refresh token, YYYYYYY is client_id and ZZZZ is client_secret, all gotten in the Netatmo app)
curl -X POST https://api.netatmo.com/oauth2/token -H "Content-Type: application/json" -d '{"grant_type":"refresh_token","refresh_token":"XXXXXXXX","client_id":"YYYYYYYY","client_secret":"ZZZZ"}'
I also tried with -H "Content-Type: application/x-www-form-urlencoded" but it does not make any difference
Here is the equivalent Berry code
cl = webclient()
cl.begin('https://api.netatmo.com/oauth2/token')
cl.add_header('Content-Type', 'application/json')
r = cl.POST('{"grant_type":"refresh_token","refresh_token":"XXXXXX","client_id":"YYYYYYY","client_secret":"ZZZZZZZ"}')
print('Refresh token - Result code: ',r)
s = cl.get_string()
print(s)
I also tried to pass the parameters in the URL, not in the body, but doesn't work either
curl -X POST "https://api.netatmo.com/oauth2/token?grant_type=refresh_token&refresh_token=XXXXXXXX&client_id=YYYYYYYY&client_secret=ZZZZZZZZZ" -H "Content-Type: application/x-www-form-urlencoded" -H "Content-Length:0"
Also tried with SoapUI but always same issue.
What's wrong ?
I looked at the other "Invalid grant_type" posts but could not find any solution to my problem
You might not know Berry but if I could make it work with curl I could certainly make it work with Berry too
Thank you
Comments
7 comments
Hello,
I'm just a user, may be @Leslie the community manager will go there in a short time.
She exchanged a lot on this thread: https://helpcenter.netatmo.com/hc/fr/community/posts/23896792864658-API-homesdata-and-homestatus
It was very helpful for me.
I create php script to manage token automaticaly (via crontab in my case), If it can help you, have a look there: https://github.com/Phil353556/netatmo-manage-tokens
Phil
Hello Phil,
Thank you for your reply.
I had already had a look on the post you mentioned and also to your php script but unfortunatly it didn't help me.
Have you been able to refresh the token from curl Windows command (in command prompt) ? If yes, could you please share the syntax you used and in particular if you passed the parameters in the URL or in the body and how you entered the strings (with " or ' or \" or \' or a mix) ?
I suspect, without being sure, that the '|' in the middle of the refresh token value is misinterpreted by curl (on Windows) and Berry.
Not all languages handle strings the same way.
Hello,
I'm really sorry.
I don't use windows anymore since decades. I only use linux (ubuntu on PC and Debian GNU/Linux on raspberry pi).
May be others persons who will read, can help you?
Phil
Hi,
@Phil, many Thx for netatmo-manage-tokens! It works like a charm ;)
@martin.gailly, here is the working cURL cmd line for refresh token on Windows (verified under PowerShell 7.5 & cURL 8.13, -v only for verbose output...)
PS> curl -v https://api.netatmo.com/oauth2/token -F "grant_type=refresh_token" -F "refresh_token=rt*************************************" -F "client_id=ci*************************************" -F "client_secret=cs*************************************"
Eureka !!!
After many many tries, I finally figured out how to make it work / the syntax to use.
The paramaters have to be passed in the body (not as URL parameters), as string (not Json), and with a '&' between each parameter
In Berry that makes :
var url_token = "https://api.netatmo.com/oauth2/token"
var post_data = "grant_type=refresh_token" + "&refresh_token=" + refresh_token + "&client_id=" + client_id + "&client_secret=" + client_secret
cl = webclient()
cl.begin(url_token)
cl.add_header('Content-Type', 'application/x-www-form-urlencoded')
r = cl.POST(post_data)
If I may, the documentation is not very clear….
Hello,
Good to know you found the solution!
Phil
Hello @nono303,
Really happy to know that the script can help you, it was the goal of sharing such script :-)
Thanks
Phil
Please sign in to leave a comment.