28 July 2010

Upload Files to a Box.net account using its API in vbscript via QTP

Box.net is a premium online file storage service and provides its API free of cost to developers. After having used a free SMS API for sending SMS via QTP, I was now destined to upload files using Box.net's API in Quick Test Professional. It took me a few days to figure out the entire process as the sample codes were not available for use in vb-script.

Before we begin to understand the code, i will discuss as to how you can get the free API access from Box.net. All you need is to visit http://www.box.net/developers and signup and once you have logged in, click on the "Start New Application" from the right hand side navigation bar and follow the instructions. This will ask you about the application you wish to develop and some other information. Fill all the details after which you will see the listed API key there. Note down the API key.


Uploading files to Box.net requires four steps - 
  • Getting the Ticket using a predefined URL with your API Key
  • Authenticating user account using a predefined URL which has the Ticket you got above.
  • Getting the Authorization Token using a predefined URL which has your API Key and the Ticket you got before.
  • Uploading file using a URL which only has the Authorization Token you got in the previous step.
The API from Box.net does a lot more stuff than what i have demonstrated. You can upload files to a specific folder, set sharing preferences, distribute the files using multiple emails, upload multiple files at a time and many more such functions. I have demonstrated the functionality where i am uploading a PDF file to the root folder. Once you are able to reach this level, you can do anything with the Box.net API.

One of the main hurdles here was that the Box.net API allows uploading files using the HTTP POST method using HTML form which was a problem when implementing it in QTP. So instead of doing that i have utilized  the DotNetFactory Object in QTP to access the System.Net.Webclient Class of the .Net Framework to upload the file. This allows you to upload the file in just two lines of code rather than creating a HTML form and doing a submit.

Code:

 api_key = "Your API key from Box.net"
 BoxNetUserName = "Your Box.net username"
 BoxNetPwd = "Your Box.net Password"

 UploadFileBoxNet("c:\coupons.pdf") 

 Function UploadFileBoxNet(filePath)

  Print filepath
    
      Set WinHttpReq = CreateObject("Msxml2.ServerXMLHTTP")

  '**********Requesting Ticket
   strURL = "https://www.box.net/api/1.0/rest?action=get_ticket&api_key=" & api_key
   temp = WinHttpReq.Open("POST", strURL, false)
   WinHttpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
   WinHttpReq.Send()
   getTicket = WinHttpReq.ResponseText
   Print strURL
   Print getTicket
  '**********Extracting Ticket from XML Response********
   getTicket = Left(getTicket,Len(getTicket)-20)
   getTicket = Right(getTicket,Len(getTicket) - Instr(getTicket,"t>")-1)
   Print getTicket

  '****Opening Authentication Window for Box.net********
   Systemutil.Run "iexplore", "https://www.box.net/api/1.0/auth/" & getTicket
   Browser("name:=Box.net.*").WebEdit("name:=login").Set BoxNetUserName
   Browser("name:=Box.net.*").WebEdit("name:=password").Set BoxNetPwd
   Browser("name:=Box.net.*").WebElement("name:=Login","height:=25").Click
   Browser("name:=Box.net.*").Sync
   Browser("name:=Box.net.*").Close
  
  '*****Requesting Auth_Token
   strUrl = "https://www.box.net/api/1.0/rest?action=get_auth_token&api_key=" api_key & "&ticket=" & getTicket
   temp = WinHttpReq.Open("POST", strURL, false)
   WinHttpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
   WinHttpReq.Send()
   Auth_Token = WinHttpReq.ResponseText
   Print strURL

  '******Parsing Auth Token from XML Response
   Set xmlObj = XMLUtil.CreateXML()
   xmlObj.Load Auth_Token
   Print xmlObj.ToString()
   Print " ========================================================================= "
   Set myToken = xmlObj.ChildElementsByPath("/response/auth_token")
   Print myToken.Count
   For i = 1 to myToken.Count
      Auth_Token = myToken.Item(i).Value()
      Print Auth_Token
      Print " "
   Next

   strURL = "https://upload.box.net/api/1.0/upload/" & Auth_Token & "/0"

  '*****Calling the Upload File method of the .Net Class System.Net.WebClient
   Set wcl = DotNetFactory.CreateInstance ("System.Net.WebClient")
   strResult = wcl.UploadFile(strURL, filepath)

 End Function


Do comment about your thoughts on this article or if there is something that you think can be improved in the code or even if you have any queries.

2 comments:

  1. What's the deal with SystemUtil.Run. Every example I can find of it on the web shows it just as above, yet when I try to call it I get a 'SystemUtil' is not declared error. Is there a namespace I should be adding that every single example on the web neglects to mention?

    ReplyDelete
  2. Thise is command for run application from QTP script

    ReplyDelete