-
-
Save anonymous/b54508d94015fe356329c5ac0f8839c9 to your computer and use it in GitHub Desktop.
Python 2.x script to add multiple users to cloud9 team via line break separated list of email addresses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# -*- coding: utf-8 -*- | |
# This code is public domain. | |
# sample command | |
# $ python adduser.py YOURTEAMNAMEHERE users.txt | |
# (users.txt should be one email per line) | |
# Also, must set env vars C9_USER_NAME and C9_PASSPHRASE | |
# Run these two commands in the terminal to safely enter your credentials before running the script: | |
# $ read -p "Username: " C9_USER_NAME && read -sp "Password: " C9_PASSPHRASE && echo "" | |
# $ export C9_USER_NAME=$C9_USER_NAME && export C9_PASSPHRASE=$C9_PASSPHRASE | |
# Be careful if your password contains \ (mine did) as even though use of r'' string will make \ raw, | |
# C9 parser will not see \ unless you escape with \ so write \\ when setting your environment variable | |
# (if you set env from bash, you need to escape that too, so you would do | |
# user@linux:~ $ export C9_PASSPHRASE=my\\\\pass | |
# if your original password has one \ symbol between my and pass | |
# Imports | |
import urllib2, os, sys, re | |
from time import sleep | |
from cookielib import CookieJar | |
# Set constants from args | |
TEAM_NAME = sys.argv[1] | |
with open(sys.argv[2]) as f: | |
file_data = f.read() | |
EMAIL_ADDRESSES = re.split(r"[\r\n]{1,2}", file_data.strip()) | |
# Set constants from environment variables | |
USER_NAME = os.environ['C9_USER_NAME'] # add login credentials to environment variables of team admin | |
PASSPHRASE = os.environ['C9_PASSPHRASE'] # change to sys.argv[3] etc. if you want to pass them in bash | |
# Initialize the CookieJar to handle cookies for urllib2 | |
cj = CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
urllib2.install_opener(opener) | |
# Data is posted to cloud9 as a text data of a JSON object in the body. | |
login_data = r'{"username": "' + USER_NAME + '", "password": "' + PASSPHRASE + '"}' | |
# Create the request object. data is not None so this is POST request | |
req = urllib2.Request("https://c9.io/auth/login", | |
data=login_data) | |
# Set some headers... some of these may not be necessary | |
req.add_header("User-Agent","kinoshitajona/c9community/script/v0.0.2") | |
req.add_header("Content-Type","application/json") | |
req.add_header("Referer","https://c9.io/login") | |
# Send the request here. We don't need anything from the response, just the cookies which are handled for us. | |
result1 = urllib2.urlopen(req) | |
# Second request, this time we need a token this one is GET request as data=None | |
req = urllib2.Request("https://c9.io/api/nc/auth?client_id=profile_direct&responseType=direct&login_hint=&immediate=1") | |
# Set headers again. | |
req.add_header("User-Agent","kinoshitajona/c9community/script/v0.0.2") | |
req.add_header("Content-Type","application/json") | |
# Send GET request | |
result2 = urllib2.urlopen(req) | |
# Read the token from the response. | |
token = result2.read() # Token | |
for ADD_USER_EMAIL in EMAIL_ADDRESSES: | |
# text JSON data with the users email which you want to invite | |
email_data = r'{"email": "' + ADD_USER_EMAIL + '"}' | |
# this is a POST request to invite the email address to your team. | |
req = urllib2.Request("https://api.c9.io/user/org/" + TEAM_NAME + "/invite?access_token=" + token, | |
data=email_data) | |
# Headers again, note the Referer uses the team name. | |
req.add_header("User-Agent","kinoshitajona/c9community/script/v0.0.2") | |
req.add_header("Content-Type","application/json") | |
# Send the POST request | |
result3 = urllib2.urlopen(req) | |
print "Added: " + ADD_USER_EMAIL | |
# Sleep on it, give the server a rest. | |
sleep(0.7) # each request takes about 1.3 seconds | |
# 0.7 seconds to not bombard the server. Each request should take 2 seconds total. | |
# If you get to this line, the user should be added. | |
# Otherwise, this python script will throw an error to bash. | |
# I have not included any error handling logic in this script. | |
print "Success: Added the above users to Cloud9 team: " + TEAM_NAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment