Monthly Archives: November 2014

Python , RESTClient and vCloud Director REST API Series for Beginners – Just login and take token

Today is 16 November 2014 and i m so new to python, RESTClient and vCloud Director API. This article is for beginners , hope help a lot for newbies

Update : in this article i have a bad view about coding , next article have better and if something wrong pls read it too .

First start from RESTClient

Before start we need to learn about little REST ,Methods and Headers . Please read when you are free this and thisquicktips and methods .

I do not have any programming background because of that explaining everything with my way. We have well known protocol which is HTTP/HTTPS and  using it for make some query and execute tasks . Softwares like vCloud Director provide us interface to do it. Methods , actually we are using each day it via web browsers , if you like to see web site actually your are doing HTTP GET and if you would like to fill out some forms you are making HTTP POST. Headers are very important here , actually we mostly know header information like “User-Agent” , “X-Forwarded-For” , list to headers

Okay, ready now . Please download RESTClient plagin for Firefox and execute it

Screen Shot 2014-11-11 at 08.00.08

Now to make something with REST API on vCloud Director , we need to login first and  after provide credentials vCloud Director will provide us a token to use it next calls .

First change the Method to POST and write down you vCloud Director url , example https://your_vclouddirector_url/api/sessions

Second, i told you headers are very important , you will see now , on top pls click Headers and then Custom Header

Screen Shot 2014-11-11 at 08.15.44

then Name part type Accept and Value part application/*+xml;version=5.5

Version part should need to be change for which version vCloud Director you are using , in my example i am using 5.5 , you can use 5.1 then type it like that
Here also i wonder what is accept and related value , accept is client site (means we are) requesting something and accept only xml base content-type, here little special thing for vCloud Director . Check headers explanations

Screen Shot 2014-11-11 at 08.17.11

Now need to add our credentials for authenticate and get token from vCloud Director

Again from the top, select Authentication and choose Basic (you could do that add like custom header what we did before)

Screen Shot 2014-11-11 at 08.26.34

Now its time to provide your credentials , here don’t forget user name should be like user@organization , here if you use System Administrator to do something you have to use admin_name@System , RESTClient will convert it base64 encoding automatically. Base64 is

Screen Shot 2014-11-11 at 08.34.54

and we are ready to go please click SEND and see the response

Screen Shot 2014-11-11 at 08.36.14

important part is x-vcloud-authorization response header , this is very important because for 30 mins it will be our key to query without credential . This article is cover only How to get Token , now do it with python , also not for you if you see any ” its quotas , i don’t know why its appearing like that 😦

Token Request Python Code

# Welcome to Python
# Python is very easy to use, no need to use $ to set veriables and use , ; end of the line.

# urllib2 is module, actually its a another code, have some functions inside. Used for web browser like functionality inside code.
# We are importing such modules to use inside our own code(to call , run and get result)

import urllib2

# base64 is module, job is endoing and decoding , we need this when provide password to connect vCloud Director

import base64

# Using import cause need to use functions with module name, means to use urllib2, you need to call module like urllib2.request
# If you import module like ‘from urllib2 import xxx” means you can use xxx function only, no whole code will be loaded. Ex: request
# Setting vCloud Director API URL, use your own one ! https://your_vcloud_url/api/
# This link will be used for make next calls after we have a token
# You can set the stings inside “” or ”
# For numbers no need to set value between “” or ”, just type it a = 5
# u is indicating its a unicode , here its not important but you should need it when using different languages letters like Turkish

vcloudapi_url = u”https://your_vcloud_url/api/”

# Setting vCloud Director Session URL
# This link will be used for only get a token

vcloudsession_url = “https://your_vcloud_url/api/sessions”

# vCloud Pass,  use your own pass !

vcloud_pass = “Sit_lord@System:password”
# Now, remember RESTClient API and vCloud Director API needs. We have to set headers inside the request
# vCloud Director version, mine is 5.5 , if you have different one change it like 5.1

vcloudapi_version = “application/*+xml;version=5.5”

# You know RESTClient automaticly encoded authentication values to base64, now do it with python

convertto_base64 = base64.b64encode(vcloud_pass)

# Encoded value will be used for authenication, set the encoded value to vcloudapi_auth veriable

vcloudapi_auth = “Basic %s” % convertto_base64

# We have a empty veriable, which name is data. actually it used for make POST request

data = “”

# Creating headers for our web request
# Actually we are creating key and value pairs here

authheaders = { ‘Accept’ : vcloudapi_version , ‘Authorization’ : vcloudapi_auth}
# Now using urllib2 module Request function, combining url , method and headers to generate how we will request something from vCloud Director

request_format = urllib2.Request(vcloudsession_url, data, authheaders)

# Send request

make_request = urllib2.urlopen(request_format)

# Read web server answer

result =

# Take the token header from response of web server
# x-vcloud-authorization is header
# This token will be used for next requests , after that no need to send credenitals to vCloud Director for 30 mins , this is default
# info() — return the meta-information of the page, such as headers
# Additional getcode() and geturl() can be used, getcode() return the HTTP status code of the response
# geturl() return the URL of the resource retrieved, commonly used to determine if a redirect was followed

token =[‘x-vcloud-authorization’]

# write out token

print token

Then you should see something like that

Screen Shot 2014-11-16 at 16.11.41

 Now time to understand functions and turn the code with function

# for create a function you need to define it like below, start with def and then function name and give the parameters what you need to have, of course you should not need to get something you can leave it like this ()

# to use this function need to feed it with related parameters, otherwise it will be not work
# Sorry maybe it will not appear correct but the code block within every function starts with a colon (:) and is indented.

def get_vcloud_token (vcd_hostname,username,password,api_version):

import urllib2
import base64

vcloudsession_url = “https://” + vcd_hostname + “/api/sessions”
encode_credentials = base64.b64encode(password)

#Prepare headers we will post
vcloudapi_version = “application/*+xml;version=” + api_version
vcloudapi_auth = “Basic %s” % encode_credentials
data = “”
authheaders = { ‘Accept’ : vcloudapi_version , ‘Authorization’ : vcloudapi_auth}
request_format = urllib2.Request(vcloudsession_url, data, authheaders)
make_request = urllib2.urlopen(request_format)
result =
token =[‘x-vcloud-authorization’]
#this function will turn result to get and use it where it called
return token

# This is another function defined and name is main, you can see that inside main function we are calling get_vcloud_token function

def main():


print token_result

# To execute main function type the name() without or with parameters
# Now main function will be called and inside main another funtion will be called too !

Now, thats it , next articles will be more more easy …
Also i will try to find out another way to share the code to be more clear …