You are on page 1of 8

Collaboration SEVT May 2016

Collaboration API Lab

Spark Provisioning via Text file Guide


Version 0.3

Collaboration SEVT
May 2016

Cisco Collaboration API Lab 2016 Page 1 of 8


Collaboration SEVT May 2016

Contents

Contents .................................................................................................................................... 2
Prepare yourself ........................................................................................................................... 3
Topics......................................................................................................................................... 3
Introduction: What does it do? .............................................................................................. 4
Step 1. Configure Script ......................................................................................................... 4
Step 2. Import Python Lybraries ........................................................................................... 4
Step 3. Read Text File .............................................................................................................. 5
Step 4. Create Spark Room .................................................................................................... 6
Step 5. Add Users to Spark Room ......................................................................................... 6
Step 6. Execute the code ........................................................................................................ 6

Updates
0.1 first version
0.2 updates, added Gold Lab instructions
0.2b minor updates, removed test email addresses
0.2c minor updates
0.3 added editor instructions, added JSON response after running code, switched to all double quotes

Cisco Collaboration API Lab 2016 Page 2 of 8


Collaboration SEVT May 2016

Prepare yourself
IF you already have installed Python, continue from step 5 Install OpenPyXL library

1. Create a Spark account by entering your email address on this Spark website
2. Get your access token from Spark for Developers
3. Install Python 3.x (below we use 3.4.4, you could also use a newer version)
OSX
Download https://www.python.org/ftp/python/3.4.4/python-3.4.4-macosx10.6.pkg
- Run installation package
Windows
Download 64-bit https://www.python.org/ftp/python/3.4.4/python-3.4.4.amd64.msi
Download 32-bit https://www.python.org/ftp/python/3.4.4/python-3.4.4.msi
- Save and run the MSI package, use default install folder C:\Python34
- Add this folder to the Windows %PATH% variable
- Control panel, System, Advanced tab, Environment Variables button,
- Select PATH and click edit. Append C:\Python34 to the existing string.
4. Install the Requests Library
OSX
install Requests for Python 3, use these command line commands:
$ sudo curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py
$ sudo pip3 install requests
Windows
pip comes with Python 3.4 Windows
- Open a command line window and change to the C:\Python34\Scripts\ folder
- Execute: pip3.4 install requests

Reference Info
Official Python 3.4 installation documentation: https://docs.python.org/3.4/installing/
Official Requests library documentation: http://docs.python-requests.org/en/latest/

Topics
1. Configure Python Script
2. Read text file
3. Create room
4. Add users to room

Cisco Collaboration API Lab 2016 Page 3 of 8


Collaboration SEVT May 2016

Editing Python code


With Python IDLE (Python editor) you may have problems copy pasting code from this
document into the editor (everything will be copied into one single line).
Workaround: Copy/paste into a different editor first and then copy/paste into IDLE.
Example editors: OSX: Athom, TextWrangler, TextEdit WINDOWS: Notepad, Notepad++

OR (my preference), dont use Pythons IDLE but use a different text editor.
My favorites: OSX: Athom, TextWrangler WINDOWS: Notepad++

Executing a Python script via the command line:


MAC: $ python yourscript.py
WIN: C:\python yourscript.py

Introduction: What does it do?


- Open text file and read all entries
- Create Spark Room
- Add all email addresses from text file to Spark Room

Step 1. Configure Script


myToken="YOUR_PERSONAL_ACCESS_TOKEN_HERE"

1. myToken: you can get your personal access token from the Spark developer
site:
Go to https://developer.ciscospark.com and login
Click the icon/avatar on the top right to see your token.

Step 2. Import Python Lybraries


Create a new file and paste the code below.

import json
import requests

Cisco Collaboration API Lab 2016 Page 4 of 8


Collaboration SEVT May 2016

import sys

1. Import json - to process the JSON data returned from the API call to Spark
2. Import requests - to send an receive data from the Spark APIs
3. Import sys - systems library to get parameters and read files

Step 3. Read Text File


The python script needs 2 parameters: the text file and the Room Name

$ Python Spark-python-importusers.py userlist.txt "My New Room"

The code below will capture the parameters and store them in 2 variables. The last line of code
reads all lines in the text file and puts the result in variable email
argLen = len(sys.argv)
if argLen < 3 :
print("Usage: "+sys.argv[0]+" <email file> <room title>")
exit(0)
emailFile = sys.argv[1]
roomTitle = sys.argv[2]
# Read the email file and save the emails in an list
emails = [line.strip() for line in open(emailFile)]

Result of the code above:


emailFile = "userlist.txt"
roomTitle = "My New Room"

Contents / prepare / intro / step 1 / step 2 / step 3 / step 4 / step 5 / step 6

Cisco Collaboration API Lab 2016 Page 5 of 8


Collaboration SEVT May 2016

Step 4. Create Spark Room


With the all email addressess available we have to create the spark room (variable: roomTitle)

headers = { "Authorization": "Bearer "+myToken,


"Content-type": "application/json" }

# Set the HTTP request payload (action)


roomInfo = { "title": roomTitle }

# Execute HTTP POST request to create the Spark Room


r = requests.post("https://api.ciscospark.com/v1/rooms",headers=headers,
json=roomInfo)
room = r.json()
print(room)

Step 5. Add Users to Spark Room


This code will browse through the cells, starting at the first row & column as defined in the
variables.

for email in emails:


# if it's an blank line don't add:
if email=="": continue
# Set the HTTP request payload (action)
membershipInfo = { "roomId": room["id"],
"personEmail": email }
# Execute HTTP POST request to create the Spark Room
r=requests.post("https://api.ciscospark.com/v1/memberships",
headers=headers, json=membershipInfo)
membership = r.json()
print(membership)
print()

Contents / prepare / intro / step 1 / step 2 / step 3 / step 4 / step 5 / step 6

Step 6. Execute the code


Make sure you have email addresses in the text file and run the script.
Run the python script with 2 variables.

Cisco Collaboration API Lab 2016 Page 6 of 8


Collaboration SEVT May 2016

$ Python Spark-python-importusers.py userlist.txt "My New Room"

Create Spark Room: JSON Response

{
"created": "2016-03-22T16:22:50.745Z",
"title": "My New Room",
"isLocked": False,
"type": "Group"
"lastActivity": "2016-03-23T09:25:08.217Z",
"id": "Y2xxx29zcGFyazovL30YS0xMDMZ1LTlhY2ItZWQwxxxkMzRlMDU2",
}

Add user: JSON Response

{
"created": "2016-03-22T16:22:50.745Z",
"isMonitor": "False",
"PersonId": "Y2xxx29zcGFyazovL30YS0xMDMZ1LTlhY2ItZWQwxxxkMzRlMDU2",
"isModerator": False,
"type": "Group"
"isModerator": False,
"personDisplayName": "apilab2016@gmail.com",
"personEmail": "apilab2016@gmail.com",
"RoomId": "Y2xxx29zcGFyazovL30YS0xMDMZ1LTlhY2ItZWQwxxxkMzRlMDU2",
}

Contents / prepare / intro / step 1 / step 2 / step 3 / step 4 / step 5 / step 6

Cisco Collaboration API Lab 2016 Page 7 of 8


Collaboration SEVT May 2016

CONGRATULATIONS !
You just completed the
Spark User Provisioning Lab
MORE INFO: http://cs.co/collabapi (Cisco internal link)

Cisco Collaboration API Lab 2016 Page 8 of 8

You might also like