经典的asp使用addheader进行授权来调用api(classic asp calling an api using addheader for authorization)

编程入门 行业动态 更新时间:2024-10-11 19:24:00
经典的asp使用addheader进行授权来调用api(classic asp calling an api using addheader for authorization)

我有一个调用api的页面,在测试模式下不需要任何授权。 我们现在正转向一个需要用户名和密码的实时环境。

api提供程序发送了以下消息:

要访问这些服务,请通过添加以下HTTP标头发送请求。 授权:基本Base64Encode(“用户名:密码”)

我不确定正确的语法,并想知道是否有人可以帮助我。

原始呼叫(并且完美地工作)是:

Dim xmlobj, username, password username="myusername" password="mypassword" Set xmlobj = server.CreateObject("MSXML2.DOMDocument.3.0") xmlobj.async = false xmlobj.setProperty "ServerHTTPRequest", True xmlObj.AddHeader "Authorization", "Basic", Base64Encode(username & ":" & password) xmlobj.load(sUrl)

上面的代码抛出了一个错误

/api-test.asp |20|800a000d|Type_mismatch:_'Base64Encode'

任何帮助将不胜感激。

I have a page that calls an api that in test mode has not required any authorization. We are now moving to a live environment where a username and password will be required.

The api provider has sent the following message:

To access these services please send the requests by adding following HTTP header. Authorization: Basic Base64Encode(“username: password”)

I'm not sure of the correct syntax and wondered if someone could help me out.

The original call (and working perfectly) is:

Dim xmlobj, username, password username="myusername" password="mypassword" Set xmlobj = server.CreateObject("MSXML2.DOMDocument.3.0") xmlobj.async = false xmlobj.setProperty "ServerHTTPRequest", True xmlObj.AddHeader "Authorization", "Basic", Base64Encode(username & ":" & password) xmlobj.load(sUrl)

The above code throws an error

/api-test.asp |20|800a000d|Type_mismatch:_'Base64Encode'

Any help would be greatly appreciated.

最满意答案

就像我在评论中提到的那样, Authorization标头的语法不正确。

API示例不期望"Base64Encode('username','password')"这是API提供的示例,向您展示如何获取字符串"username:password"和Base64编码它,这是Authorization标头期待。

但您仍需要使用Base64Encode()函数定义才能使代码Base64Encode() 。

Base64Encode()和MyASC()函数取自Base64编码VBS函数(vb编码器算法),源代码

这样的事情应该有效;

<%
Function Base64Encode(inData)
  'rfc1521
  '2001 Antonin Foller, Motobit Software, http://Motobit.cz
  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim cOut, sOut, I

  'For each group of 3 bytes
  For I = 1 To Len(inData) Step 3
    Dim nGroup, pOut, sGroup

    'Create one long from this 3 bytes.
    nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
      &H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))

    'Oct splits the long To 8 groups with 3 bits
    nGroup = Oct(nGroup)

    'Add leading zeros
    nGroup = String(8 - Len(nGroup), "0") & nGroup

    'Convert To base64
    pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)

    'Add the part To OutPut string
    sOut = sOut + pOut

    'Add a new line For Each 76 chars In dest (76*3/4 = 57)
    'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
  Next
  Select Case Len(inData) Mod 3
    Case 1: '8 bit final
      sOut = Left(sOut, Len(sOut) - 2) + "=="
    Case 2: '16 bit final
      sOut = Left(sOut, Len(sOut) - 1) + "="
  End Select
  Base64Encode = sOut
End Function

Function MyASC(OneChar)
  If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function

Dim xmlobj, username, password
username="myusername"
password="mypassword"
Set xmlobj = server.CreateObject("MSXML2.XMLHTTP.3.0")
xmlobj.Open "GET", sUrl, False
xmlobj.setRequestHeader "Authorization", "Basic " & Base64Encode(username & ":" & password)
xmlobj.Send

Dim xmldoc
If xmlobj.status = 200 Then
  Set xmldoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
  xmldoc.Load xmlobj.ResponseXML
Else
  Response.Write "An error occurred!"
End If
%>

Like I've mentioned in the comments, the syntax for the Authorization header is incorrect.

The API example doesn't expect "Base64Encode('username','password')" this is an example supplied by the API to show you how to take the string "username:password" and Base64 encode it which is what the Authorization header is expecting.

But you still need to have the Base64Encode() function definition for the code to work.

Base64Encode() and MyASC() functions are taken from Base64 encode VBS function (vb encoder algorithm), source code

Something like this should work;

<%
Function Base64Encode(inData)
  'rfc1521
  '2001 Antonin Foller, Motobit Software, http://Motobit.cz
  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim cOut, sOut, I

  'For each group of 3 bytes
  For I = 1 To Len(inData) Step 3
    Dim nGroup, pOut, sGroup

    'Create one long from this 3 bytes.
    nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
      &H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))

    'Oct splits the long To 8 groups with 3 bits
    nGroup = Oct(nGroup)

    'Add leading zeros
    nGroup = String(8 - Len(nGroup), "0") & nGroup

    'Convert To base64
    pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)

    'Add the part To OutPut string
    sOut = sOut + pOut

    'Add a new line For Each 76 chars In dest (76*3/4 = 57)
    'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
  Next
  Select Case Len(inData) Mod 3
    Case 1: '8 bit final
      sOut = Left(sOut, Len(sOut) - 2) + "=="
    Case 2: '16 bit final
      sOut = Left(sOut, Len(sOut) - 1) + "="
  End Select
  Base64Encode = sOut
End Function

Function MyASC(OneChar)
  If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function

Dim xmlobj, username, password
username="myusername"
password="mypassword"
Set xmlobj = server.CreateObject("MSXML2.XMLHTTP.3.0")
xmlobj.Open "GET", sUrl, False
xmlobj.setRequestHeader "Authorization", "Basic " & Base64Encode(username & ":" & password)
xmlobj.Send

Dim xmldoc
If xmlobj.status = 200 Then
  Set xmldoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
  xmldoc.Load xmlobj.ResponseXML
Else
  Response.Write "An error occurred!"
End If
%>

                    
                     
          

更多推荐

本文发布于:2023-07-17 13:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:经典   addheader   asp   api   calling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!