Learning content is available on Cisco DevNet (https://developer.cisco.com/site/devnet/home/index.gsp) (DevNet> Collaboration> Spark for Developers has three Learning Labs) ..
It's carefully crafted, so if you follow the steps, you'll learn how to access Cisco Spark via REST-API using POSTMAN.
The next step here is to summarize how to change POSTMAN to Python.
An Access Token is required to access Cisco Spark via REST-API. An Access Token is an authentication string given to an individual when registering with Cisco Spark. Get it in the following ways:
https://developer.ciscospark.com/ Access and log in.
If you log in successfully, an avatar (an icon with a human face) will be displayed in the upper right corner.
Click on the avatar to see the Access Token. Access Token is information that should never be known to others, so you need to be careful when handling it.
Reference: Cisco Spark API Reference (https://developer.ciscospark.com/endpoint-rooms-get.html)
You can run http GET under the following conditions:
Use requests to perform an HTTP GET in Python. The code is, for example:
import requests
access_token = 'Enter your Access Token here'
url = 'https://api.ciscospark.com/v1/rooms'
headers = {
'Authorization' : 'Bearer ' + access_token,
'Content-Type' : 'application/json'
}
r = requests.get(url, headers = headers)
print(r.json())
If you just want to retrieve the Room ID, change print (r.json ()) on the last line to:
for line in r.json()['items']:
print line['id']
To get the Message, use the Room ID obtained in Experiment 1 and execute http GET under the following conditions:
Below is a sample script that displays the user's email ID and message:
# -*- coding: utf-8 -*-
import requests
urlr = 'https://api.ciscospark.com/v1/rooms'
urlm = 'https://api.ciscospark.com/v1/messages'
headers = {
'Authorization' : 'Bearer ' + access_token,
'Content-Type' : 'application/json'
}
r = requests.get(urlr, headers = headers)
for line in r.json()['items']:
payload = {'roomId' : line['id']}
print '################### Room: ', line['title']
r2 = requests.get(urlm, headers = headers, params = payload)
try:
for mline in r2.json()['items']:
try:
print mline['personEmail'], '>>>', mline['text']
except KeyError:
pass
except KeyError:
pass
Recommended Posts