[Solved]Connection.Send(), how to send data as dict(json) Topic is solved

Python and python framework

Moderator: leecollings

Post Reply
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

[Solved]Connection.Send(), how to send data as dict(json)

Post by Flopp »

I have created a plug-in to get data from an API, but almost every hour it print out an error that the hardware has been ended unexpectedly.

After looking at other plug-ins I found that I have put all my code in onHeartbeat, other plug-in are using onConnect, onMessage etc.

So I tried to rewrite my code but I didn’t get so far before I got a problem.


I am trying to get a token using an API.

I can get the token using requests.request in a separate python script.

The data(payload) to be sent with Connection.Send() must be as unicode string, byte or byte array.

I have low knowledge about creating a plug-in.

Code: Select all


 payload = {
                       'grant_type':'refresh_token',
                       'client_id':'319d92371dhdhd3d3782',
                       'client_secret': 'QoSJ/k9Ky/ddX4RX45V7Ohdud2L53=',
                       'refresh_token': '4AM5!_eBNb9yxjUuwQdNeN0EQEaBt6LA49o4fxB6Tw'
            }

            headers = { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Host': 'api.nibeuplink.com' }
            
            Connection.Send({'Verb': 'POST', 'URL':'/oauth/token', 'Headers': headers, 'Data': payload})

The API is for NibeUplink

Scope is not required
Attachments
A3257EBD-4571-437D-A530-A4C93A0EA835.jpeg
A3257EBD-4571-437D-A530-A4C93A0EA835.jpeg (201.25 KiB) Viewed 481 times
Last edited by Flopp on Tuesday 24 November 2020 22:45, edited 2 times in total.
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: Connection.Send(), how to send data as json

Post by Flopp »

Solved it

instead of a dict I just used as below

'Data':''grant_type=refresh_token&client_id=319d92371dhdhd3d3782&client_secret=QoSJ/k9Ky/ddX4RX45V7Ohdud2L53=&refresh_token=4AM5!_eBNb9yxjUuwQdNeN0EQEaBt6LA49o4fxB6Tw"
Post Reply