Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 229602

Help with python script for vcloud director api

$
0
0

Hi ,

 

I have written a python script that logs me in to my vcloud director environment, however I am struggling to run any api calls after successfully logging in.

 

import urllib2
import base64

def test(username, org, password, key, secret):

  
# make a string with the request type in it:
  method
="POST"
  
# create a handler. you can specify different handlers here (file uploads etc)
  
# but we go for the default
  handler
= urllib2.HTTPSHandler()
  
# create an openerdirector instance
  opener
= urllib2.build_opener(handler)
  
# build a request
  url
='https://api.vcloud-site.com/api/vcd/sessions'
  request
= urllib2.Request(url)
  request
.add_header("Accept","application/*+xml;version=5.1")
  request
.add_header("Authorization","Basic "+ encode_auth(username, org, password))
  request
.add_header("x-id-sec", encode_key(key, secret))

  
# overload the get method function with a small anonymous function...
  request
.get_method =lambda: method
  
# try it; don't forget to catch the result
  
try:
  connection
= opener.open(request)
  
except urllib2.HTTPError,e:
  connection
= e

  
# check. Substitute with appropriate HTTP code.
  
if connection.code ==200:
  data
= connection.read()

  
print"Data :", data

  
else:
  
print"ERRROR", connection.code

def encode_auth(username, org, password):
  
return base64.b64encode(username +"@"+ org +":"+ password)

def encode_key(key, secret):
  
return base64.b64encode(key +":"+ secret)

username
='oli'
org      =
'vCD'
password
='mypassword'
key     
='xxx'
secret  
='xxx'

test
(username, org, password, key, secret)


I get a Session response which details the api version, user, api endpoint and the session token response. My question is do I have to use that session token response when I do a get or post response and how would I do this in the script above?


Many thanks,

Oli


Viewing all articles
Browse latest Browse all 229602

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>