Controlling a function with python?

anyone had any luck with python triggering a function ?.
I can use https - GET to work but not a https - POST like this :smile:

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”
}

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

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

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

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)

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 .