peter_a
December 14, 2015, 6:34pm
1
anyone had any luck with python triggering a function ?.
I can use https - GET to work but not a https - POST like this
h = httplib2.Http()
resp, content = h.request("https://api.particle.io/v1/devices/220034001447343338333633?access_token= "+token , method="GET" )
print content
which prints this
{
"id": "220034001447343338333633",
"name": "turtle_laser",
"connected": true,
"variables": {},
"functions": [
"blue"
],
"cc3000_patch_version": "wl0: Nov 7 2014 16:03:45 version 5.90.230.12 FWID 01-dccc7d34",
"product_id": 6,
"last_heard": "2015-12-14T18:23:35.408Z"
}
But if I try a using a POST to control the blue function I`ve played all day and still can't get it to work.
data = {'args' : '1' , 'access_token' : token}
body = urllib.urlencode(data)
h = httplib2.Http()
resp, content = h.request("https://api.particle.io/v1/devices/220034001447343338333633/blue " , method="POST" , body=body )
print content
{
"error": "invalid_request",
"error_description": "The access token was not found"
}
bko
December 14, 2015, 6:39pm
2
Hi @peter_a
You need to pass the args and token in the headers, not the data part of the POST.
Try something more like this (you may need to pass an empty body too, I didnât try it):
headers = {'args' : '1' , 'access_token' : token}
h = httplib2.Http()
resp, content = h.request("https://api.particle.io/v1/devices/220034001447343338333633/blue" , method="POST" , headers=headers )
print content
peter_a
December 14, 2015, 7:04pm
3
still not it , still get the :-
âerrorâ: âinvalid_requestâ,
âerror_descriptionâ: "The access token was not found"
but if I add ?access_token=âbjghjhhjhjâ to the end of blue I get a .
{
âidâ: â220034001447343338333633â,
âlast_appâ: ââ,
âconnectedâ: true,
âreturn_valueâ: 0
}
now , with means I`m still not passing any of the args.
Try changing the name of the header (equivalent to the curl -H
method in the docs):
headers = {'Authorization' : 'Bearer ' + token}
Iâm not sure why the urlencoded data didnât work.
Edit: The urlencoded data needs a corresponding header setting the appropriate content-type:
headers={'content-type':'application/x-www-form-urlencoded'}
2 Likes
peter_a
December 14, 2015, 8:53pm
5
spot on !!!!!!
data = {'arg' : '1' , 'access_token' : token}
headers={'content-type':'application/x-www-form-urlencoded'}
body = urllib.urlencode(data)
h = httplib2.Http()
resp, content = h.request("https://api.particle.io/v1/devices/220034001447343338333633/blue " , method="POST" , body=body , headers=headers )
print content
{
"id": "220034001447343338333633",
"last_app": "",
"connected": true,
"return_value": 11
}
3 Likes
jroo
December 17, 2015, 3:50am
6
If you donât mind an external library, Requests makes it dead simple:
import requests
device_id = 'your device id'
access_token = 'your access token'
args = 'your args'
r = requests.post('https://api.particle.io/v1/devices/%s/functionName' % device_id, data={'args': args, 'access_token':access_token})
print (r.text)
peter_a
December 17, 2015, 9:24am
7
Thanks for that , it is a couple of years since iâve done any serious python programming, so having to remember which libraries to use .