2013-12-16 37 views
0

是否可以使用需要從Excel進行身份驗證的WebAPI? 對於常規的.Net客戶端,我們將身份驗證憑證發送到WebAPI的令牌端點,並獲取在隨後的調用中作爲承載連接的令牌。 但是,這是如何從Excel中完成的?什麼對象需要在VBA代碼中使用?從Excel中使用安全的ASP.Net WebAPI

WebAPI返回需要在Excel中顯示爲列和行的對象的集合。

+0

[如何在Excel 2010中使用身份驗證使用Web API]的副本(http://stackoverflow.com/q/18119393/456814)。 – 2014-08-14 06:47:16

回答

1

你還記得XHR,還是應該說XML HTTP Request :)?

這裏是我寫上我的一個項目爲例,用戶調用Web服務從Oracle數據庫返回的數據進行驗證:

Dim xhr As Object 

On Error Resume Next 
Set xhr = CreateObject("MSXML2.XMLHTTP") 

If Err.Number <> 0 Then 
    Set xhr = CreateObject("MSXML.XMLHTTPRequest") 
    Application.StatusBar = "Error 0, has occured while creating a MSXML.XMLHTTPRequest object" 
End If 

If xhr Is Nothing Then 
    Application.StatusBar = "For some reason it wasn't possible to make a MSXML2.XMLHTTP object" 
    Exit Function 
End If 

'consuming the web service. 
xhr.Open "POST", "/webservice/url/goes/here", False 
xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
xhr.send "post parameters can be sent here format: paramname=paramvalue&paramname2=paramvalue2" 

If xhr.Status = 200 Then  
    'creating the XmlDocument to load the received data. 
    Dim xDoc As MSXML2.DOMDocument60 
    Set xDoc = CreateObject("Msxml2.DOMDocument.6.0") 
    xDoc.setProperty "ProhibitDTD", False 
    xDoc.setProperty "ResolveExternals", True 

    'loading the data into the in-memory xml document. 
    xDoc.validateOnParse = True 
    xDoc.async = False 
    xDoc.LoadXML xhr.responseText 
    Application.StatusBar = False 

    'xDoc now holds your web service returned data 
End If 

上面的代碼可以用來連接到一個Web服務。 在這個鏈接http://www.mcpher.com/Home/excelquirks/snippets/basicauth有關於如何實現基本認證的更多細節。

希望這可以幫助你,

問候。