使用Visual Basic访问Box.com文件

时间:2012-03-15 18:29:00

标签: vb.net box-api

有没有人知道是否有使用Visual Basic连接到Box.com文档的示例代码?

我们有一些精心制作的VB Excel宏,可以从各种文件中提取数据。现在我们已经切换到使用box.com作为我们的文件系统,我们希望这些宏能够读取/写入Box.com中存储的文件所需的数据。

1 个答案:

答案 0 :(得分:2)

以下是一些示例代码,用于将VBScript中的文件上传到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