【问题标题】:Providing authentication info via msxml2.ServerXMLHTTP通过 msxml2.ServerXMLHTTP 提供身份验证信息
【发布时间】:2013-12-20 22:02:05
【问题描述】:

我正在使用 Classic ASP 并尝试使用 JustGiving API。

我想用它在我的网站上的捐款页面上显示筹集的总金额和收到的捐款总额。

我可以通过以下方式查看该信息: https://api.justgiving.com/docs/resources/v1/Account/Retrieve

<%
vurl = "http://api.justgiving.com/---myIDhere---/v1/account"
Set http = Server.CreateObject("msxml2.ServerXMLHTTP")
http.Open "GET", vurl, False
http.Send

Set dom = Server.CreateObject("msxml2.DOMDocument")

dom.loadXML http.responseText

Set items = dom.getElementsByTagName("account")

For Each item In items

    Set var_totalDonated = item.getElementsByTagName("totalDonated")
    If NOT (var_totalDonated IS Nothing) Then
        var_totalDonated = ap(totalDonated(0).Text)
        response.write var_totalDonated
    End If

Next
%>

但是,当我访问它时页面超时。

我认为这是因为我需要提供一些身份验证信息,如下所述: https://api.justgiving.com/docs/usage#protectedResources

所以我得到了那个身份验证信息。

但我不知道如何将其“发送”到 API,以便它可以验证我的用户身份并提供信息。

它还提到通过此链接上方的链接提供有关标题的信息(我无法发布链接,因为我没有足够的声誉),但将 URL 末尾的 #protectedResources 替换为 #contentTypes。

对不起 - 我也错过了那方面的东西吗?

如果我问了一些愚蠢的问题,我很抱歉,但 API 文档中的信息假定用户具有一定程度的智能,而我没有很多!

非常感谢任何建议。

谢谢


感谢约翰的回复。

基于此,我将代码改为:

<%
vurl = "https://api.justgiving.com/API_KEY/v1/account"
Set http = Server.CreateObject("msxml2.ServerXMLHTTP")
http.Open "GET", vurl, False, "username", "pwd"
http.setTimeouts 5000, 5000, 10000, 10000 ''ms - resolve, connect, send, receive
http.setRequestHeader "Authorization", "Basic MY_AUTH_STRING"
http.Send

Set dom = Server.CreateObject("msxml2.DOMDocument")

dom.loadXML http.responseText

Set items = dom.getElementsByTagName("account")

For Each item In items

    Set var_totalDonated = item.getElementsByTagName("totalDonated")
    If NOT (var_totalDonated IS Nothing) Then 
        var_totalDonated = (var_totalDonated(0).Text)
        response.write var_totalDonated
    End If

Next
%>

但不幸的是页面仍然超时。

我也在通过以下方式进行检查: groups.google.com/forum/#!topic/justgiving-api/Xhz5Fkxuy1s

但目前还没有答案。

再次感谢


固定版本

<%

Sub debug( varName )
    Dim varValue
    varValue = Eval( varName )
    response.write "<p style='margin:10px; border-bottom:2px solid #ccc;border-top:1px solid #eaeaea;background-color:white;padding:10px;color:red;text-align:left;'><strong>" & varName & "</strong>: " & varvalue & "</p>" & vbcrlf & vbcrlf
End Sub

vurl = "https://api.justgiving.com/AP_KEY/v1/account"
Set http = Server.CreateObject("msxml2.ServerXMLHTTP")
http.Open "GET", vurl, False, username, password
http.setTimeouts 5000, 5000, 10000, 10000 'ms - resolve, connect, send, receive
http.setRequestHeader "Authorization", "Basic AUTH_STRING"
http.Send

Response.ContentType = "application/xml"

Set dom = Server.CreateObject("msxml2.DOMDocument")

dom.loadXML http.responseText

Set items = dom.getElementsByTagName("account")

For Each item In items

    Set var_totalDonated = item.getElementsByTagName("totalDonated")
    If NOT (var_totalDonated IS Nothing) Then 
        var_totalDonated = ap(var_totalDonated(0).Text)
        debug "var_totalDonated"
    End If

    Set var_totalRaised = item.getElementsByTagName("totalRaised")
    If NOT (var_totalRaised IS Nothing) Then 
        var_totalRaised = ap(var_totalRaised(0).Text)
        debug "var_totalRaised"
    End If

    Set var_totalGiftAid = item.getElementsByTagName("totalGiftAid")
    If NOT (var_totalGiftAid IS Nothing) Then 
        var_totalGiftAid = ap(var_totalGiftAid(0).Text)
        debug "var_totalGiftAid"
    End If

Next
%>

以前我用的是:

vurl = "https://api.justgiving.com/AP_KEY/v1/account"

但是当我将其更改为 https 时,它起作用了。

我以为我以前尝试过,但显然没有。

再次感谢约翰,非常感谢您的帮助!

【问题讨论】:

    标签: api asp-classic


    【解决方案1】:

    试试

    http.Open "GET", vurl, False, "yourusername", "yourpassword"
    

    我不知道这是否适用于 justgiving,但它适用于 Bing API

    另外,这个问题可能是相关的 XmlHttp Request Basic Authentication Issue

    编辑 - 使用 Response.ContentType 和 Msxml2.ServerXMLHTTP.6.0

    vurl = "https://api.justgiving.com/API_KEY/v1/account"
    Set http = Server.CreateObject("msxml2.ServerXMLHTTP.6.0")
    http.Open "GET", vurl, False, "username", "pwd"
    http.setTimeouts 5000, 5000, 10000, 10000 'ms - resolve, connect, send, receive'
    http.setRequestHeader "Authorization", "Basic MY_AUTH_STRING"
    http.Send
    
    Response.ContentType = "application/xml"    
    
    Set items = http.responseXML.getElementsByTagName("account")
    

    【讨论】:

    • 感谢约翰的帮助。我基于此再次更新了页面,但页面仍然超时:-(我已经编辑了上面的帖子以显示更新。希望它快到了 - 我已经通过 API Google Groups 页面询问过,看看他们是否可以也有帮助。再次感谢
    • 再说一次,我不知道这是否会有所不同,但请尝试在 Server.CreateObject 语句中使用“Msxml2.ServerXMLHTTP.6.0”和“Msxml2.DOMDocument.6.0”,特别是如果你'重新使用 IIS7 或 8 - 这意味着您使用的是最新版本的 msxml。一旦你让它工作,我建议你看看服务器端 XSLT 转换,Classic ASP 做得相当好,而不是直接解析,但首先让你的 http 请求工作 - 一次一步
    • 另外,尝试 Response.ContentType 而不是创建一个新的 XML 对象,我已经编辑了我的答案来告诉你我的意思
    • 嗨,约翰,非常感谢您的帮助。在您的帮助下,我得到了它,并编辑了我的原始帖子以显示工作代码。最后,一旦我在 JustGiving 端连接到 https 而不是 http,它就起作用了。再次感谢:-)
    【解决方案2】:

    抱歉添加到旧帖子。然而,当谷歌搜索 MailChimp API V3.0 和 VBA 的帮助时,这种情况一直出现。

    这是我使用的修复:

    ret = objhttp.Open("POST", sURL, False) 
    objhttp.setRequestHeader "Content-Type", "application/json"
    objhttp.setRequestHeader "Accept", "application/json"
    
    'V3 API uses HTTP Basic Authorisation inside an https: wrapper.
    'The standard windows method does not seem to work however the 
    'following hack does.
    'In summary the user name and APIkey are seperated with a Colon: and 
    'base 64 encoded and added to a Http RequestHeader
    
    
    objhttp.setRequestHeader "Authorization", "Basic " & Base64Encode(APIUser & ":" & ApiKey)
    
    objhttp.send (sJson)
    

    您需要对 Base64Encode 函数进行编码。我从http://pastie.org/1192157 (Ex StackOverflow) 中获取了一些代码并将其粘贴到 VBA 模块中。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-05
      • 2018-08-25
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多