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 http://www.vmware.com 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

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

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

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)

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

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

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 &#8221; 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

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

# 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 = make_request.info()[‘x-vcloud-authorization’]

# write out token

print token

Then you should see something like that

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.