Pregunta

Does anyone know if there is an sample code for using Visual Basic to connect to Box.com documents?

We have some elaborate VB Excel macros that pull data in from various files. Now that we have switched over to using box.com as our file system, we would like these macros to read/write the required data to files stored in Box.com.

¿Fue útil?

Solución

Here is some sample code for uploading a file in VBScript to Box:

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

source

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top