How to get published events using python

I’ve been trying to subscribe to events from my Photon using python, but I’m having some trouble.
Code on my photon is the standard “publish and dashboard with photoresistors” sketch.

Using curl works

curl -H "Authorization: Bearer {ACCESS_TOKEN_GOES_HERE}" \
https://api.particle.io/v1/events/motion-detected

Yields results like

event: motion-detected
data: {"data":"intact","ttl":"60","published_at":"2015-06-25T05:08:22.136Z","coreid":"coreid"}

event: motion-detected
data: {"data":"broken","ttl":"60","published_at":"2015-06-25T05:08:23.014Z","coreid":"coreid"}
But I can't get the same to work using requests 

However, I’ve tried using requests:

address3 ='https://api.particle.io/v1/events/beamStatus'
data = {'Authorization': 'Bearer {ACCESS_TOKEN_GOES_HERE}'}
r3 = requests.get(address3, headers=data)

And I get nothing.
So, then I took a look at : Subscribe to spark events using python
and tried :

from sseclient import SSEClient 
messages = SSEClient('https://api.spark.io/v1/events?access_token={acces-token-goes-here}')
for msg in messages:
     print(str(msg).encode('utf-8'))

And I get a response, but it’s weird, like it’s not from my Photon

{"data":"b:-3;rb:41;d:0;rd:73;mo:0;m:2022;h:1932;c:2105;l:0","ttl":"60","published_at":"2015-06-27T20:15:08.802Z","coreid":"53ff6a066667574853222067"}
{"data":"75.65","ttl":"60","published_at":"2015-06-27T20:15:08.893Z","coreid":"53ff6e066667574836061267"}
{"data":"{ \"s\":\"wthr\", \"u\":\"F\",\"l\":\"Houston, TX\",\"m\":\"Temperature\",\"o\":\"DXHackers\",\"g\":\"10000000-0000-0000-0000-000000000001\",\"v\": 71.599998,\"d\":\"Paul Office\" }","ttl":"60","published_at":"2015-06-27T20:15:08.941Z","coreid":"54ff6d066672524844560167"}
{"data":"{ \"s\":\"wthr\", \"u\":\"%\",\"l\":\"Houston, TX\",\"m\":\"Humidity\",\"o\":\"DXHackers\",\"g\":\"10000000-0000-0000-0000-000000000001\",\"v\": 39.000000,\"d\":\"Paul Office\" }","ttl":"60","published_at":"2015-06-27T20:15:08.946Z","coreid":"54ff6d066672524844560167"}
{"data":"{\"Hours\": 11, \"Minutes\": 21, \"Seconds\": 57}","ttl":"60","published_at":"2015-06-27T20:15:09.020Z","coreid":"54ff6a066678574941340667"}
{"data":"{\"Watt1\":2289, \"Watt2\": 1576, \"Watt3\": 1887}","ttl":"60","published_at":"2015-06-27T20:15:09.024Z","coreid":"54ff6a066678574941340667"}
{"data":"Light: 255","ttl":"60","published_at":"2015-06-27T20:15:09.047Z","coreid":"53ff6d065067544820560587"}
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 27-28: ordinal not in range(128)

Is that something I should expect? It doesn’t look like it’s talking to my photon?!

Other than that weirdness, what’s the best way to subscribe to these private published events from the spark?

Do you ever have that moment, when you post something on a forum and realize what you screwed up almost immediately after?

should be

messages = SSEClient('https://api.spark.io/v1/events/beamStatus?access_token={access token}')

d’oh!
Now it works :slight_smile:

1 Like

I tired this but whenever i run the program, i get error 401 - unauthorized for url.

Your access token has to be provided without the curly braces.

Thanks i forgot to remove the ‘{}’. I am getting a typeerror though -

TypeError: the JSON object must be str, not 'bytes'

How do i resolve this?

So in python 3 you have decode the encoded variable as it appears in bytes. so i used

dataJson = json.loads(data.decode('utf-8'))

now it says expected value error.

PS - im not really good at Python